c++ - 删除 XML 文件 C++ 中的子节点

标签 c++ xml pugixml

我正在使用 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/

相关文章:

c++ - Pugixml - 解析具有前缀映射和不带前缀映射的命名空间

c++ - 知道我的顶点是否(以及如何)被重新排序

c++ - 将 openGL 上下文保存为视频输出

c++ - 在 C++ 中动态创建函数调用

java - 泛型XmlAdapter可以写吗

python - 当相同标签位于 XML 中的子树中时,使用 Python 获取唯一值

c++ - 在 Pugixml 中获取 XPath 查询的行/列

c++ - 有没有办法将模板函数签名作为模板模板参数传递

xml - 如何在 Odoo 上使用 attrs 属性将多个属性(不可见、只读或必需)组合成一个?

pugixml - 使用 pugixml 保留空文本标签