I want to remove the JOB node with JOBNAME "One" from the following file:
<SETTINGS>
<SYSTEM>
<VERSION>3.1<VERSION>
</SYSTEM>
<JOB>
<JOBNAME>One</JOBNAME>
</JOB>
<JOB>
<JOBNAME>Two</JOBNAME>
</JOB>
</SETTING>
The code that is supposed to remove it looks like this:
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(str_fileName));
System.out.println("starting parser");
NodeList list = doc.getElementsByTagName("*");
for (int i = 0; i <
list.getLength(); i++) {
//Get Node
Node node = (Node) list.item(i);
// Look through entire settings file
if (node.getNodeName().equalsIgnoreCase("JOB")) {
NodeList childList = node.getChildNodes();
// Look through all the children
for (int x = 0; x < childList.getLength(); x++) {
Node child = (Node) childList.item(x);
// Only the JOB children will be looked in.
System.out.println("looking for JOBNAME and " + jobName);
if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equalsIgnoreCase("JOBNAME") && child.getTextContent().equalsIgnoreCase(jobName)) {
System.out.println(" found JOBNAME and " + jobName);
// Delete job node here
//node.getParentNode().removeChild(node);
doc.getRootElement().getChild("row").removeChild("address");
}
}
}
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException saxe) {
saxe.printStackTrace();
}
When I run the code, it doesn't throw any exceptions, but it doesn't actually remove the node either.
Note that I want to remove the JOB node, as well as all its contents.
Can anyone help with this?