I need to remove the node/nodes completely when a specific child element is found in nodes of XML For Instance, My XML is as follows:
<?xml version="1.0"?>
<booklist>
<book>
<name>THEORY OF DYNAMICS</name>
<author>JOHN</author>
<price>09786</price>
</book>
<book>
<name>ABCD</name>
<author>STACEY</author>
<price>765</price>
</book>
<book>
<name>ABCD</name>
<author>BTYSON</author>
<price>34974</price>
</book>
<book>
<name>ABCD</name>
<author>CTYSON</author>
<price>09534</price>
</book>
<book>
<name>INTRODUCING JAVA</name>
<author>CHARLES</author>
<price>1234</price>
</book>
<book>
<name>ABCD</name>
<author>TYSON</author>
<price>34534</price>
</book>
So,When i search for book tag ='ABCD' my result should be as follows:
OUTPUT XML:
<?xml version="1.0"?>
<booklist>
<book>
<name>THEORY OF DYNAMICS</name>
<author>JOHN</author>
<price>09786</price>
</book>
<book>
<name>INTRODUCING JAVA</name>
<author>CHARLES</author>
<price>1234</price>
</book>
The code which i tried is as follows:
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(FILENAME));
NodeList list = doc.getElementsByTagName("*");
for (int i = 0; i <list.getLength(); i++) {
Node node = (Node) list.item(i);
// Searching through entire file
if (node.getNodeName().equalsIgnoreCase("book")) {
NodeList childList = node.getChildNodes();
// Looking thhrough all children nodes
for (int x = 0; x < childList.getLength(); x++) {
Node child = (Node) childList.item(x);
// To search only "book" children
if (child.getNodeType() == Node.ELEMENT_NODE &&
child.getNodeName().equalsIgnoreCase("name") &&
child.getTextContent().toUpperCase().equalsIgnoreCase("abcd".toUpperCase())) {
// Delete node here
node.getParentNode().removeChild(node);
}
}
}
}
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMParser parser = new DOMParser();
parser.parse(FILENAME);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(NEWFILE));
transformer.transform(source, result);
} catch (IOException io) {
io.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException saxe) {
saxe.printStackTrace();
}
I'm unable to delete all the book nodes which has child element as "abcd" , Instead am able to delete only few alternative book nodes which has child element as "abcd". Can you suggest me what is the mistake in my code? Why am i unable to delete all the book nodes whose name='abcd'?
No comments:
Post a Comment