我想改变"A“在苹果,在键的值是1到"D",在这个代码中的2项应该被替换。我试过这样做,但什么也没发生。
<C>
<B>
<Key value="1">
<Apple>A</Apple>
</Key>
<Key value="2">
<Apple>A</Apple>
</Key>
<Key value="3">
<Apple>A</Apple>
</Key>
<Key value="1">
<Apple>A</Apple>
</Key>
</B>
</C>
Qt:
QDomNodeList roots = doc.elementsByTagName("Key");
for (int i = 0; i < roots.size(); ++i) {
QDomElement domElement = roots.at(i).toElement();
QDomAttr attribute = domElement.attributeNode("value");
if (attribute.value() == "1") {
domElement.firstChild().setNodeValue("D");
}
发布于 2017-02-01 12:16:38
在您的代码中,domElement.firstChild()
将<Apple>
元素节点而不是引用到其内容。<Apple>
内部的值是也是a节点(文本节点),这就是为什么它位于DOM层次结构中的一个更深的层次:
domElement
- <Key>
domElement.firstChild()
- <Apple>
domElement.firstChild().firstChild()
- A
因此,您还必须获得<Apple>
元素节点的第一个子节点:
domElement.firstChild().firstChild().setNodeValue("D");
发布于 2017-02-01 11:30:38
也许:
if (attribute.value() == "1")
https://stackoverflow.com/questions/41988025
复制相似问题