我必须将eElement
中找到的与旧值匹配的值更改为另一个值。
我尝试使用eElement.setAttribute(...)
函数和setTextContent
函数,但它不起作用。
如果我们假设新值存储在名为newValue
的String变量中,我该如何运行代码?
NodeList leaf = doc.getElementsByTagName(relativeLeaf);
System.out.println(leaf.item(0).getNodeName());
for (int temp = 0; temp < leaf.getLength(); temp++) {
Node nNode = leaf.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String oldValueInCells = eElement.getElementsByTagName(relativeLeaf).item(0).getTextContent();
System.out.println("old tag : " + eElement.getElementsByTagName(relativeLeaf).item(0).getTextContent());
if(oldValueInCells.contentEquals(oldVal)){
// ####
// here i have to change tha value in eElement
// where it match with the old Value with a new one
}
}
}
答案 0 :(得分:0)
基于文档,我认为您可以获取节点元素,然后设置一个值。首先应该得到这样的节点元素:
Node node = eElement.getElementsByTagName(relativeLeaf).item(0); //You get the node here so you can use it as well to create OldValueInCells
String oldValueInCells = node.getTextContent();
System.out.println("old tag : " + oldValueInCells);
if(oldValueInCells.contentEquals(oldVal)){
// ####
// here i have to change tha value in eElement
// where it match with the old Value with a new one
node.setTextContent("new value"); //Here you will set the value to the node
}
来源:https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html