简体   繁体   English

如何在Java中修改XML标签?

[英]How to modify XML tag in Java?

I have xml file with tag as following : 我有带有标签的xml文件,如下所示:

<sometag/>

I want to modify this tag as following : 我想按以下方式修改此标签:

<sometag>somevalue</sometag>

initially I tried to delete element and create new element, that doesnt work. 最初,我尝试删除元素并创建新元素,但这不起作用。 then I get to know that there is already a method to rename a tag, I tried following code, but dont know how to add value (somevalue) for this renamed element(sometag). 然后我知道已经有一种重命名标签的方法,我尝试了以下代码,但不知道如何为该重命名的元素(sometag)添加值(somevalue)。

NodeList scpNodes=doc.getElementsByTagName("sometag");
    for (int i = 0; i < scpNodes.getLength(); i++) {
        Element toBeReplaced=(Element) scpNodes.item(i);
        doc.renameNode(toBeReplaced, toBeReplaced.getNamespaceURI(),"sometag");        
     // how to append value ?
    }

you have to call 你必须打电话

toBeReplaced.setTextContext("someValue");

and get rid of 并摆脱

doc.renameNode(toBeReplaced, toBeReplaced.getNamespaceURI(),"sometag");  

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

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