I would like to delete a child from an XML file and of course save the file after the modification. Here is my XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Customers>
<Marker>
<title>ABB EMEA</title>
</Marker>
<Marker>
<title>AllScripts</title>
</Marker>
<Marker>
<title>ARRIS (Motorola)</title>
</Marker>
<Marker>
<title>ARRIS (RWC)</title>
</Marker>
<Marker>
<title>BHS</title>
<site_location>Weinhammer, Hofhalde, Konstanz, Germany</site_location>
</Marker>
<Marker>
<title>Durst</title>
<site_location>Brixen, Italy</site_location>
</Marker>
<Marker>
<title>EMEA DEMO</title>
<site_location>AWS could</site_location>
</Marker>
<Marker>
<title>Harris</title>
</Marker>
</Customers>
I would like to complete remove the child that has the 'title' - 'Durst'.
Here is my code:
public static void Rebuild_Cords_XML (File ff)
{
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(ff);
NodeList nodes = doc.getElementsByTagName("Marker");
int x=0;
for(int i=0;i<nodes.getLength();i++)
{
Node nNode = nodes.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
if(eElement.getElementsByTagName("title").item(0).getTextContent().equals(("Durst")));
{
}
}
}
// write the DOM object to the file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer=null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(ff);
try {
transformer.transform(domSource, streamResult);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (ParserConfigurationException | IOException | SAXException e) {
;}
}
As you can see I am able to detect it manually, but I do not know who to remove it, trying to access a parent node deletes other 'Marker' children but not the required one.
Please assist.
Markerhavingtitleequals to Durst? or just title tag?