简体   繁体   English

删除标记并使用Java更改XML中的行?

[英]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. 我有一个XML,我需要使用Java从其中删除一个完整的标签。 As of now there is not specific position of that tag, it can be anywhere but it will be inside client tag. 截至目前,该标签没有特定的位置,它可以在任何位置,但将在client标签内。

<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. 我需要从上述XML中完全删除此<derta-config>标记。 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> . 在同一XML文件中,该行<hello>collect_model = 1</hello>实例只有一次。 Below world block can be anywhere and nested as well within various other world block. world下方的world可以在任何地方,也可以嵌套在其他各个world And it will be inside client tag as well but there might be other nesting as well. 它也将在client标签内,但可能还会有其他嵌套。

<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. 我需要像这样写这行: <hello>collect_model = 0</hello>在同一world块中。 So my final XML will not have above <derta-config> tag and collect_model will be 0 所以我的最终XML将没有上面的<derta-config>标记,并且collect_model将为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);
}

Next you call document.getElementsByTagName("derta-config") , iterate the list (should be one long, right?), and use node.getParentNode().removeChild(node) . 接下来,调用document.getElementsByTagName("derta-config") ,对列表进行迭代(应该是一个长行,对吗?),然后使用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) . 之后,调用document.getElementsByTagName("hello") ,遍历列表,使用node.getTextContent()检查文本内容,如果它是您要更改的值,则可以使用node.setTextContent(newValue)进行更改。 。

Then you save the result back to a file. 然后,将结果保存回文件。 See How to save parsed and changed DOM document in xml file? 请参阅如何将经过解析和更改的DOM文档保存在xml文件中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM