I have a document org.w3c.dom.Document and i want to replace the value of a particular tag in xml.I have tried following but somehow it doesnot work.It doesnot give error but i cant see the change in value.
org.w3c.dom.Document
public boolean SetTextInTag(Document doc, String tag, St开发者_如何转开发ring nodeValue)
{
Node node = getFirstNode(doc, tag);
if( node != null){
node.setNodeValue(nodeValue);
return true;
}
return false;
}
EG
<mytag> this value is to be changed </mytag>
I want the tag value to be chnaged to nodeValue.My code doesnot give any error but i cant see the change in value.
Try node.setTextContent(nodeValue)
instead of node.setNodeValue(nodeValue)
.
Using setNodeValue
on a node to change it's value will work, but only if it is a Text node. In all likelihood, the setNodeValue
method was invoked on a Node that was not a text node. In reality, your code might be modifying an Element node, and therefore not having any result.
To explain this further, your document:
<mytag> this value is to be changed </mytag>
is actually seen by the parser as:
Element (name = mytag, value = null)
- TextNode (name = #text, value= " this value is to be changed ")
Element nodes will always have a value of null, so setting the value on them will not modify the value of the child text node. Using setTextContent as suggested in one of the answers will work, for it modifies the value of the TextNode instead of the Element.
You could also use setNodeValue to change the value, but only after detecting if the node is a TextNode:
if (node != null && node.getNodeType() == Node.TEXT_NODE) {
node.setNodeValue(nodeValue);
return true;
}
You need to write the xml-file out to see the changes. Do you do that?
The value of a node is not necessarily what you think it is. Take a look at the table here: documentation
You might be better of using the replaceChild
function for your purposes. That is
Node newChild = document.createTextNode("My new value");
Node oldChild = // Get the child you want to replace...
replaceChild(newChild, oldChild);
Remember, that what you are trying to replace is a text node that is the child of the tag you just looked up. So likely, Node oldChild = node.getFirstChild;
is what you are looking for.
Look into this code... Might help you out.
<folks>
<person>
<name>Sam Spade</name>
<email>samspade@website.com</email>
</person>
<person>
<name>Sam Diamond</name>
<email>samdiamond@website.com</email>
</person>
<person>
<name>Sam Sonite</name>
<email>samsonite@website.com</email>
</person>
</folks>
Here is the code to parse and update the node value...
public void changeContent(Document doc,String newname,String newemail) {
Element root = doc.getDocumentElement();
NodeList rootlist = root.getChildNodes();
for(int i=0; i<rootlist.getLength(); i++) {
Element person = (Element)rootlist.item(i);
NodeList personlist = person.getChildNodes();
Element name = (Element)personlist.item(0);
NodeList namelist = name.getChildNodes();
Text nametext = (Text)namelist.item(0);
String oldname = nametext.getData();
if(oldname.equals(newname)) {
Element email = (Element)personlist.item(1);
NodeList emaillist = email.getChildNodes();
Text emailtext = (Text)emaillist.item(0);
emailtext.setData(newemail);
}
}
}
精彩评论