gpt4 book ai didi

c++ - 如何使用qt删除嵌套的xml元素

转载 作者:行者123 更新时间:2023-11-30 01:57:21 45 4
gpt4 key购买 nike

我有这样的xml文件

<root>
<element>
<child id = "0"> Some Text </child> <-- Target To Delete
</element>
<element>
<child id = "1"> Some Text </child>
</element>
</root>

如何删除 id 为“0”的子元素?使用 Qt 库。

最佳答案

QDomDocument doc;
doc.setContent(oldXml);

QDomNodeList nodes = doc.elementsByTagName("element");
for (int i = 0; i < nodes.count(); ++i)
{
QDomNode node = nodes.at(i);
QDomElement child = node.firstChildElement("child");
if (!child.isNull() && child.attribute("id") == "0")
{
node.removeChild(child);
}
}

QString newXml = doc.toString();

关于c++ - 如何使用qt删除嵌套的xml元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18820702/

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