java 更改 xml 标记的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6303798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the value of a xml tag
提问by akshay
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,我想替换 xml 中特定标签的值。我尝试了以下操作,但不知何故它不起作用。它没有给出错误,但我看不到值的变化。
org.w3c.dom.Document
org.w3c.dom.Document
public boolean SetTextInTag(Document doc, String tag, String 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.
我希望将标记值更改为 nodeValue。我的代码没有给出任何错误,但我看不到值的变化。
回答by dogbane
Try node.setTextContent(nodeValue)
instead of node.setNodeValue(nodeValue)
.
尝试node.setTextContent(nodeValue)
代替node.setNodeValue(nodeValue)
.
回答by Vineet Reynolds
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.
setNodeValue
在节点上使用来改变它的值是可行的,但前提是它是一个文本节点。很可能,该setNodeValue
方法是在不是文本节点的节点上调用的。实际上,您的代码可能正在修改 Element 节点,因此没有任何结果。
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.
元素节点的值始终为 null,因此在它们上设置值不会修改子文本节点的值。使用其中一个答案中建议的 setTextContent 会起作用,因为它会修改 TextNode 的值而不是 Element 的值。
You could also use setNodeValue to change the value, but only after detecting if the node is a TextNode:
您也可以使用 setNodeValue 来更改值,但只有在检测到节点是否为 TextNode 之后才能使用:
if (node != null && node.getNodeType() == Node.TEXT_NODE) {
node.setNodeValue(nodeValue);
return true;
}
回答by mahju
You need to write the xml-file out to see the changes. Do you do that?
您需要写出 xml 文件以查看更改。你这样做吗?
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
您最好将该replaceChild
功能用于您的目的。那是
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.
请记住,您要替换的是一个文本节点,它是您刚刚查找的标签的子节点。很有可能,Node oldChild = node.getFirstChild;
这就是您要寻找的。
回答by RajeshS
Look into this code... Might help you out.
查看此代码...可能会帮助您。
<folks>
<person>
<name>Sam Spade</name>
<email>[email protected]</email>
</person>
<person>
<name>Sam Diamond</name>
<email>[email protected]</email>
</person>
<person>
<name>Sam Sonite</name>
<email>[email protected]</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);
}
}
}