简体   繁体   中英

C++ Using RapidXml parsing XML File, Wrapper Class, parse_error expect >

I'm trying to use the RapidXML to parse my XML file. And I did it following the example here . Instead of doing the parsing in the main function, I wrote a wrapper class called XMLParser to do the parsing job. And this really gives me a headache.

The XMLParser.hpp:

#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include "rapidxml/rapidxml.hpp"

using namespace std;
using namespace rapidxml;

class XMLParser {

public:
    XMLParser() {};

    XMLParser(const std::string &xmlString): xmlCharVector(xmlString.begin(), xmlString.end())
    {
        //xmlCharVector.push_back('\0');
         parseXML();
    }
    XMLParser(const std::vector<char> &_xmlVector):xmlCharVector(_xmlVector)
    {
        /* xmlCharVector.push_back('\0'); */  // already done in main.cpp
        if (xmlCharVector != _xmlVector)      //And it turns out they're the same....
            std::cout << "The two vectors are not equal" << std::endl;
        else
            std::cout << "They are the same" << std::endl;
        parseXML();
    }

private:
    std::vector<char> xmlCharVector;
    rapidxml::xml_document<> doc;
    void parseXML();

};

The XMLParser.cpp:

#include "XMLParser.hpp"

using namespace std;
using namespace rapidxml;

void XMLParser::parseXML()
{
    doc.parse<0>(&xmlCharVector[0]);
}

And here is the main.cpp:

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include "XMLParser.hpp"

using namespace std;
using namespace rapidxml;

int main(int argc, char **argv)
{
    xml_document<> doc;
    xml_node<> *root_node;
    ifstream theFile("beer.xml");
    vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
    buffer.push_back('\0');

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

    root_node = doc.first_node("MyBeerJournal");
    xml_node<> *engine = root_node->first_node("Brewery");

    //The above code works pretty well, and I can get the element I want in XML file.

    //The problem occurs when I tried to use the XMLParser
    XMLParser xmlParser(buffer);
    return 0;
}

The parsing process in the main function works pretty well. But when I tried to use the function in my wrapper class parseXML() , then error occured:

terminate called after throwing an instance of 'rapidxml::parse_error' what(): expected > Abort (core dumped)

Originally I have other code in this function, but I commented them all, and find that even with the single line doc.parse<0>(&xmlCharVector[0]); . Why it works well in main.cpp while not in the wrapper class? I really can't figure it out. Could anybody help me?

I've found out the reason... This stupid problem really takes me a long time to debug. I'm writing it here so that anyone ran into it (hope not) could save his time. The problem lies exactly in the code doc.parse<0>(&buffer[0]) in the main function. Before executing this line of code, the buffer(type of vector<char>) is like this: (by printing the vector to console)

<MyBeerJournal>
    <Brewery name="Founders Brewing Company" location="Grand Rapids, MI">
        <Beer name="Centennial" description="IPA" rating="A+" dateSampled="01/02/2011">
            "What an excellent IPA. This is the most delicious beer I have ever tasted!"
        </Beer>
    </Brewery>
    .....
    .....
</MyBeerJournal>

It's the same with original xml file. After executing the above code, the buffer(type of vector<char>) becomes something like this:

<MyBeerJournal
    <Breweryname"Founders Brewing Company location"Grand Rapids, MI>

        <Beername"Centennial description"IPA rating"A+ dateSampled"01/02/2011>

            "What an excellent IPA. This is the most delicious beer I have ever tasted!"
        /Beer>

    </Brewery>

As you can see, some angel brackets disappeared. and some other things like double quote has also been changed. So the wrapper class constructor copied the modified "xml buffer", and this not well formatted xml vector will certainly cause the second doc.parse<0>(&xmlCharVector[0]); in the wrapper class to fail. I don't know why the library writer needs to modify the char vector passed in, because the subsequent xml analysis is not relevant to the original char vector once the DOC has been created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM