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

时间:2013-09-16 05:06:12

标签: c++ xml qt dom

我有像这样的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库。

1 个答案:

答案 0 :(得分:2)

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();