?
Solved

remove a node from xml document in Java

Posted on 2015-01-25
1
Medium Priority
?
266 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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