How to parse an XML file with RapidXml

前端 未结 4 1554
情话喂你
情话喂你 2020-12-25 14:58

I have to parse an XML file in C++. I was researching and found the RapidXml library for this.

I have doubts about doc.parse<0>(xml).

Can

4条回答
  •  情歌与酒
    2020-12-25 15:38

    We usually read the XML from the disk into a std::string, then make a safe copy of it into a std::vector as demonstrated below:

    string input_xml;
    string line;
    ifstream in("demo.xml");
    
    // read file into input_xml
    while(getline(in,line))
        input_xml += line;
    
    // make a safe-to-modify copy of input_xml
    // (you should never modify the contents of an std::string directly)
    vector xml_copy(input_xml.begin(), input_xml.end());
    xml_copy.push_back('\0');
    
    // only use xml_copy from here on!
    xml_document<> doc;
    // we are choosing to parse the XML declaration
    // parse_no_data_nodes prevents RapidXML from using the somewhat surprising
    // behavior of having both values and data nodes, and having data nodes take
    // precedence over values when printing
    // >>> note that this will skip parsing of CDATA nodes <<<
    doc.parse(&xml_copy[0]);
    

    For a complete source code check:

    Read a line from xml file using C++

提交回复
热议问题