Solved

remove a node from xml document in Java

Posted on 2015-01-25
1
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…
Suggested Courses

738 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