【问题标题】:C++/RapidXML: Edit node and write to a new XML file doesn't have the updated nodesC++/RapidXML:编辑节点并写入新的 XML 文件没有更新的节点
【发布时间】:2017-07-18 14:49:58
【问题描述】:

我正在从 string 解析 XML 文件。 我的节点Idbar,我想把它改成foo然后写入文件。

写入文件后,文件仍然有bar,而不是foo

#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
void main()
{
    std::string newXml = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?><Parent><FileId>fileID</FileId><IniVersion>2.0.0</IniVersion><Child><Id>bar</Id></Child></Parent>";

    xml_document<> doc;
    xml_node<> * root_node;

    std::string str = newXml;
    std::vector<char> buffer(str.begin(), str.end());
    buffer.push_back('\0');

    doc.parse<0>(&buffer[0]);

    root_node = doc.first_node("Parent");

    xml_node<> * node = root_node->first_node("Child");
    xml_node<> * xml = node->first_node("Id");
    xml->value("foo"); // I want to change my id from bar to foo!!!!

    std::ofstream outFile("output.xml");
    outFile << doc; // after I write to file, I still see the ID as bar
}

我在这里错过了什么?

【问题讨论】:

    标签: c++ rapidxml


    【解决方案1】:

    问题在于数据的布局。在node_element 节点xml 下还有另一个node_data 节点包含"bar"。 您发布的代码也无法编译。在这里,我编译了你的代码并展示了如何修复它:

    #include <vector>
    #include <iostream>
    #include "rapidxml.hpp"
    #include "rapidxml_print.hpp"
    
    int main()
    {
        std::string newXml = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?><Parent><FileId>fileID</FileId><IniVersion>2.0.0</IniVersion><Child><Id>bar</Id></Child></Parent>";
    
        rapidxml::xml_document<> doc;
    
        std::string str = newXml;
        std::vector<char> buffer(str.begin(), str.end());
        buffer.push_back('\0');
    
        doc.parse<0>(&buffer[0]);
    
        rapidxml::xml_node<>* root_node = doc.first_node("Parent");
    
        rapidxml::xml_node<>* node = root_node->first_node("Child");
        rapidxml::xml_node<>* xml = node->first_node("Id");
        // xml->value("foo"); // does change something that isn't output!!!!
    
        rapidxml::xml_node<> *real_thing = xml->first_node();
        if (real_thing != nullptr                         // these checks just demonstrate that
           &&  real_thing->next_sibling() == nullptr      // it is there and how it is located
           && real_thing->type() == rapidxml::node_data)  // when element does contain text data 
        {
            real_thing->value("yuck");  // now that should work
        }
    
        std::cout << doc; // lets see it
    }
    

    所以它输出:

    <Parent>
        <FileId>fileID</FileId>
        <IniVersion>2.0.0</IniVersion>
        <Child>
            <Id>yuck</Id>
        </Child>
    </Parent>
    

    看到了吗?请注意,在解析期间数据的布局方式取决于您提供给解析的标志。例如,如果您首先输入doc.parse&lt;rapidxml::parse_fastest&gt;,那么解析器将不会创建这样的node_data 节点,然后更改node_element 数据(就像您第一次尝试一样)将起作用(而我上面所做的不会)。阅读manual的详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多