代码如下:

1     QDomNodeList listFlowChart= doc.elementsByTagName("device");
2     QDomElement flowChart = listFlowChart.at(0).toElement();
3     QDomNodeList listAllNodes = flowChart.childNodes();
4     while (listAllNodes.size() > 0) {
5                         flowChart.removeChild(listAllNodes.at(0));
6     }

listFlowChart 是xml中所有device标签的集合,取其中第一个,删除其下所有子标签。最好检测一下listFlowChart是否为空。

 

不能用for循环来移除标签。因为从listAllNode中每移除一个标签,listAllNode.size()都会减一。

相关文章: