tags:

views:

134

answers:

3

In the below sample XML, how to remove Entire B Node if E=13 using java parser.

<xml>
   <A>
     <B>
       <C>
         <E>11</E>
         <F>12</F>
       </C>
    </B>
    <B>
       <C>
         <E>13</E>
         <F>14</F>
      </C>
    </B>
  </A>

Please advise.

A: 

Here you are

Fernando Miguélez
Thanks for reply,but still it doesn't satisfy my needs.
+1  A: 

It's easy if you are using DOM. Just traverse the document and keep track of the B nodes. When you hit an E=13 node, remove the B node. Here is some code to help:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new File("file.xml"));
DocumentTraversal traversal = (DocumentTraversal) doc;
Node a = doc.getDocumentElement();
NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
Element b = null;
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
    Element e = (Element) n;
    if ("B".equals(e.getTagName())) {
        b = e;
    } else if ("E".equals(e.getTagName()) && "13".equals(e.getTextContent()) && b != null) {
        a.removeChild(b);
    }
}
dogbane
Great it Works.many Thanks.
@fahd, checkout my answer to see how the Java SE XPath APIs can be used to eliminate the DocumentTraversal - http://stackoverflow.com/questions/3717215/remove-xml-node-using-java-parser/3717875#3717875
Blaise Doughan
A: 

Alternative DOM Approach

Alternatively, instead of doing a brute force traversal of the XML document you could use the XPath capabilities in the JDK to find the "B" element with value "13" and then remove it from its parent:

import java.io.File;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import org.w3c.dom.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]");

        Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
        b13Node.getParentNode().removeChild(b13Node);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.transform(new DOMSource(document), new StreamResult(System.out));
    }

}

The advantage of using an XPath it's easier to maintain, if the structure changes it's just a one line change to your code. Also if the depth of your document grows the XPath based solution stays the same number of lines.

Non-DOM Approach

If you don't want to materialize your XML as a DOM. You could use a Transformer and a stylesheet to remove a node:

Blaise Doughan
This also works good.Thanks