Solved

remove a node from xml document in Java

Posted on 2015-01-25
1
259 Views
Last Modified: 2015-01-26
I'm using  javax.xml.parsers.DocumentBuilder and would like to preprocess my xml document by removing a node  <Status > </Status> if it has
any following same siblings.

For example  <Status>   <Ins>    XXX  </Ins>      </Status>
                        <Status>    <Ins>    YYY  </Ins>    </Status>

I want to get rid of YYY
0
Comment
Question by:sniger
1 Comment
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
ID: 40570879
Give it a try with the following. Few things left for you to complete.


                  builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                  document = builder.parse(new FileInputStream(xmlFile));
                  document.getDocumentElement().normalize();
                  xpath = XPathFactory.newInstance().newXPath();
                  
                  NodeList nodeList = (NodeList) xpath.evaluate("/Status/Ins", document.getDocumentElement(), XPathConstants.NODESET);
                  for (int index=0; index<nodeList.getLength(); ++index) {
                        
                        Node node = nodeList.item(index);
                        
                                    if (node!=null && node.getNodeType()==Node.XXXXX_NODE) {
                                          // TODO do removal activity
                                    }
                  }
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 64
type mismatch (Object[] to double[] 4 33
Problem to Alipay 10 47
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 24
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question