Search code examples
javaxmldomxpathsax

remove a tag and alter a line in an XML using Java?


I have an XML in which I need to remove one whole tag from it using Java. As of now there is not specific position of that tag, it can be anywhere but it will be inside client tag.

<xml>

<client id="100" version="2">
    <!--  some stuff here -->

    <derta-config>
        <!--  some stuff here -->
    </derta-config>

    <!--  some stuff here -->

    <!--  some stuff here -->
</client>

I need to remove this <derta-config> tag altogether from the above XML. And there is one more thing. In the same XML file, I will have only once instance of this line <hello>collect_model = 1</hello>. Below world block can be anywhere and nested as well within various other world block. And it will be inside client tag as well but there might be other nesting as well.

<world>
    <world>
        <world>
            <world>
                <hello>collect_model = 1</hello>
                <hello>enable_data = 0</hello>
                <hello>session_ms = 1000</hello>
                <hello>max_collect = string_integer($extract("max_collect"))</hello>
                <hello>max_collect = parenting(max_collect, max_collect, 100)</hello>
                <hello>output('{')</hello>
            </world>
        </world>
    </world>
</world>

I need to make that line like this: <hello>collect_model = 0</hello> in the same world block. So my final XML will not have above <derta-config> tag and collect_model will be 0

<world>
    <world>
        <world>
            <world>
                <hello>collect_model = 0</hello>
                <hello>enable_data = 0</hello>
                <hello>session_ms = 1000</hello>
                <hello>max_collect = string_integer($extract("max_collect"))</hello>
                <hello>max_collect = parenting(max_collect, max_collect, 100)</hello>
                <hello>output('{')</hello>
            </world>
        </world>
    </world>
</world>

Below is my code and I am not sure what should I do to remove these things?

File fileName = new File("file example");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(fileName);

Update:-

File fileName = new File("file example");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(fileName);

NodeList configTag = document.getElementsByTagName("derta-config");
for (int i = 0; i < configTag.getLength(); i++) {
    Node node = configTag.item(i);
    System.out.println(node.getNodeName());
    node.getParentNode().removeChild(node);
}

Solution

  • Next you call document.getElementsByTagName("derta-config"), iterate the list (should be one long, right?), and use node.getParentNode().removeChild(node).

    After that, you call document.getElementsByTagName("hello"), iterate the list, check the text content using node.getTextContent(), and if it is the value you want to change, you change it with node.setTextContent(newValue).

    Then you save the result back to a file. See How to save parsed and changed DOM document in xml file?