How to parse an XML file with RapidXml

前端 未结 4 1558
情话喂你
情话喂你 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:34

    RapidXml comes with a class to do this for you, rapidxml::file in the rapidxml_utils.hpp file. Something like:

    #include "rapidxml_utils.hpp"
    
    int main() {
        rapidxml::file<> xmlFile("somefile.xml"); // Default template is char
        rapidxml::xml_document<> doc;
        doc.parse<0>(xmlFile.data());
    ...
    }
    

    Note that the xmlFile object now contains all of the data for the XML, which means that once it goes out of scope and is destroyed the doc variable is no longer safely usable. If you call parse inside of a function, you must somehow retain the xmlFile object in memory (global variable, new, etc) so that the doc remains valid.

提交回复
热议问题