我正在使用 PugiXml XML 相关操作的库。
我的 XML 文件:
<CurrentStatus>
<Time Stamp= "12:30">
<price>100</price>
<amount>1</amount>
</Time>
<Time Stamp= "14:50">
<price>10</price>
<amount>5</amount>
</Time>
<Time Stamp= "16:30">
<price>10</price>
<amount>5</amount>
</Time>
</CurrentStatus>
出于测试目的,我给出了一个硬编码值,我想删除具有属性 Stamp = 14:50
的节点 Time
。
XML 节点删除代码: 我使用了这个 SO Question作为删除节点的引用(名称 = 时间
和属性 = 14:50
)。
for (xml_node child = doc.child("CurrentStatus").first_child(); child; )
{
xml_node next = child.next_sibling();
string attributeValue = child.attribute("Stamp").as_string();
if (attributeValue == "14:50")
{
cout << "delete" << endl;
child.parent().remove_child(child);
}
child = next;
}
问题: 上面的代码运行没有任何错误。它甚至进入了 if 语句
,但为什么执行后原始 XML 文件保持不变?
PS:我确信根节点和 XML 文档在一般情况下被正确读取,因为我能够在控制台上显示 XML 结构。
最佳答案
我认为问题在于保存。
我在 if-condition
之后执行了以下保存语句,它起作用了。
doc.save_file("D:/myfile.xml");
关于c++ - 删除 XML 文件 C++ 中的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52816222/