gpt4 book ai didi

c++ - QDomNode类在qt中读取child的值

转载 作者:行者123 更新时间:2023-11-30 05:17:53 25 4
gpt4 key购买 nike

我想在 Apple 中更改“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");
}

最佳答案

在您的代码中 domElement.firstChild()指的是<Apple>元素节点,到它的内容。 <Apple> 里面的值也是一个节点 ( text node ),这就是为什么它在 DOM 层次结构中位于更深一层的原因:

  • domElement <Key>
  • domElement.firstChild() <Apple>
  • domElement.firstChild().firstChild() A

所以你还必须得到 <Apple> 的第一个 child 元素节点:

domElement.firstChild().firstChild().setNodeValue("D");

关于c++ - QDomNode类在qt中读取child的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com