2

i have the following xml :

<table1>
    <row0>
        <column1>String</column1>
        <column2>int</column2>
    </row0>

    <row1>
        <column1>aa</column1>
        <column2>65432</column2>
    </row1>

    <row2>
        <column1>ww</column1>
        <column2>1111</column2>
    </row2>

    <row3>
        <column1>fff</column1>
        <column2>333</column2>
    </row3>

    <row4>
        <column1>jj</column1>
        <column2>2</column2>
    </row4>
</table1>

and i want to delete the node row3 and its elements. I am writing that XML file in java.how to do that? i saw this code in another post but couldn't understand it

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));
}
Mark Mamdouh
  • 169
  • 4
  • 17
  • 1
    You could use [DOM](https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html) for iterating through and altering the nodes! – ParkerHalo Nov 17 '15 at 14:30
  • Possible duplicate of [Remove XML Node using java parser](http://stackoverflow.com/questions/3717215/remove-xml-node-using-java-parser) – dguay Nov 17 '15 at 14:31
  • yes , but what edits to do to help me?@dguay – Mark Mamdouh Nov 17 '15 at 14:33
  • So, if you didn't understand the code, did you read about XPath? Did you try to look up the documentation of the various classes used in that code? – RealSkeptic Nov 17 '15 at 14:40

2 Answers2

8

This code snippet should do what you want to. If you just want to remove the row3 element from your tree you don't have to use (and understand) the overmighty XPATH!

// -------------- building the document out of the file  -----------------
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));
//------------------------------------------------------------------------

// -------------- finding the right node and removing it -----------------
Element table = document.getDocumentElement();
Node row3 = table.getElementsByTagName("row3").item(0);
table.removeChild(row3);
//------------------------------------------------------------------------

// -------------- printing the resulting tree to the console -------------
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(document), new StreamResult(System.out));
//------------------------------------------------------------------------
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
0

This code will remove multiple nodes at a time and multiple occurrences of the selected node.

for (String nodeToBeRemoved : nodesToBeRemoved) {
    NodeList allNodeList = doc.getElementsByTagName(nodeToBeRemoved);
    removeSelectedNode(allNodeList);
  }

  private static void removeSelectedNode(NodeList allNodeList) {
allNodeList.item(0).getParentNode().removeChild(allNodeList.item(0));
if(allNodeList.getLength()>0)
  removeSelectedNode(allNodeList);
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36