0

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.

2
  • Do you want to remove Marker having title equals to Durst? or just title tag? Commented Mar 25, 2014 at 19:37
  • I would like to delete the Marker that "title" equals to "Durst". Commented Mar 25, 2014 at 19:40

1 Answer 1

1

I'm not entirely sure what you're trying to delete, so here's both ways.

This will remove the entire marker which contains the title durst:

        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"))) {
                    nNode.getParentNode().removeChild(nNode);
                }
            }
        }

This will remove just the title tag in the marker:

        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"))) {
                    nNode.removeChild(eElement.getElementsByTagName("title").item(0));
                }
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much the first option is the I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.