如何使用qt删除xml元素

时间:2014-06-20 10:49:01

标签: qt

我有像这样的xml文件

<Plan>
 <car id="1">491,142;492,138;</car>
 <car id="10073">498,141;489,145;</car>
 <car id="1">483,143;477,145;</car>
 <car id="10075">487,142;490,137;</car>
 <car id="10076">483,137;488,136;</car>
 <car id="10077">484,146;480,144;</car>
 <car id="1">480,147;498,142;</car>
 <car id="10079">488,143;487,147;</car>
 <car id="1">498,141;487,142;</car>
 <car id="10081">487,143;481,144;</car>
 <car id="1">495,137;485,137;</car>
 <car id="10083">486,142;484,140;</car>
 <car id="10084">478,147;479,142;</car>
 <car id="1">493,139;489,139;</car>
 <car id="10087">498,140;490,136;</car>
 <car id="10088">479,145;484,142;</car>
</Plan>

如何使用qt库删除id为“1”的car元素?

1 个答案:

答案 0 :(得分:0)

这很简单:

QDomDocument doc;
QFile file("/tmp/1.xml");
file.open(QFile::ReadOnly);
doc.setContent(&file);
QDomElement plan = doc.documentElement();
QDomNodeList cars = plan.childNodes();
for(int i = 0; i < cars.count(); i++) {
  QDomNode node = cars.at(i);
  if (node.isElement() && node.toElement().attribute("id") == "1") {
    plan.removeChild(node);
  }
}
qDebug() << doc.toString();