user3462473
user3462473

Reputation: 359

How to modify XML tag in Java?

I have xml file with tag as following :

<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).

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 ?
    }

Upvotes: 0

Views: 65

Answers (1)

Andr&#233; R.
Andr&#233; R.

Reputation: 1657

you have to call

toBeReplaced.setTextContext("someValue");

and get rid of

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

Upvotes: 1

Related Questions