Read a line from xml file using C++

后端 未结 3 2004
傲寒
傲寒 2020-12-17 06:57

My XML File has:

< Package > xmlMetadata < /Package >

I am searching for a tag in this file and the text between the startin

3条回答
  •  抹茶落季
    2020-12-17 07:39

    A single line of tags on a file can hardly be described as XML. Anyway, if you really want to parse a XML file, this could be accomplished so much easier using a parser library like RapidXML. This page is an excellent resource.

    The code below is my attempt to read the following XML (yes, a XML file must have a header):

    File: demo.xml

    
    
         xmlMetadata 
    
    

    A quick note: rapidxml is consisted only of headers. On my system I unzipped the library to /usr/include/rapidxml-1.13, so the code below could be compiled with:

    g++ read_tag.cpp -o read_tag -I/usr/include/rapidxml-1.13/

    File: read_tag.cpp

    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace rapidxml;
    
    
    int main()
    {
        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]);
    
        // alternatively, use one of the two commented lines below to parse CDATA nodes,
        // but please note the above caveat about surprising interactions between
        // values and data nodes (also read http://www.ffuts.org/blog/a-rapidxml-gotcha/)
        // if you use one of these two declarations try to use data nodes exclusively and
        // avoid using value()
        //doc.parse(&xml_copy[0]); // just get the XML declaration
        //doc.parse(&xml_copy[0]); // parses everything (slowest)
    
        // since we have parsed the XML declaration, it is the first node
        // (otherwise the first node would be our root node)
        string encoding = doc.first_node()->first_attribute("encoding")->value();
        // encoding == "utf-8"
    
        // we didn't keep track of our previous traversal, so let's start again
        // we can match nodes by name, skipping the xml declaration entirely
        xml_node<>* cur_node = doc.first_node("rootnode");
        string rootnode_type = cur_node->first_attribute("type")->value();
        // rootnode_type == "example"
    
        // go straight to the first Package node
        cur_node = cur_node->first_node("Package");
        string content = cur_node->value(); // if the node doesn't exist, this line will crash
    
        cout << content << endl;
    }
    

    Outputs:

    xmlMetadata

提交回复
热议问题