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


i'm trying use rapidxml parse xml file. , did following example here. instead of doing parsing in main function, wrote wrapper class called xmlparser parsing job. , gives me 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'); */  // done in main.cpp         if (xmlcharvector != _xmlvector)      //and turns out they're same....             std::cout << "the 2 vectors not equal" << std::endl;         else             std::cout << "they 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 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, , can element want in xml file.      //the problem occurs when tried use xmlparser     xmlparser xmlparser(buffer);     return 0; } 

the parsing process in main function works pretty well. when tried use function in wrapper class parsexml(), error occured:

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

originally have other code in function, commented them all, , find single line doc.parse<0>(&xmlcharvector[0]);. why works in main.cpp while not in wrapper class? can't figure out. me?

i've found out reason... stupid problem takes me long time debug. i'm writing here ran (hope not) save time. problem lies in code doc.parse<0>(&buffer[0]) in main function. before executing line of code, buffer(type of vector<char>) this: (by printing vector console)

<mybeerjournal>     <brewery name="founders brewing company" location="grand rapids, mi">         <beer name="centennial" description="ipa" rating="a+" datesampled="01/02/2011">             "what excellent ipa. delicious beer have ever tasted!"         </beer>     </brewery>     .....     ..... </mybeerjournal> 

it's same original xml file. after executing above code, buffer(type of vector<char>) becomes this:

<mybeerjournal     <breweryname"founders brewing company location"grand rapids, mi>          <beername"centennial description"ipa rating"a+ datesampled"01/02/2011>              "what excellent ipa. delicious beer have ever tasted!"         /beer>      </brewery> 

as can see, angel brackets disappeared. , other things double quote has been changed. wrapper class constructor copied modified "xml buffer", , not formatted xml vector cause second doc.parse<0>(&xmlcharvector[0]); in wrapper class fail. don't know why library writer needs modify char vector passed in, because subsequent xml analysis not relevant original char vector once doc has been created.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -