1

I am trying to do something like this using rapidxml using c++

xml_document<> doc; 
ifstream myfile("map.osm"); 
doc.parse<0>(myfile); 

and receive the following error

Multiple markers at this line - Invalid arguments ' Candidates are: void parse(char *) ' - Symbol 'parse' could not be resolved

file size can be up to a few mega bytes.

please help

jj 1
  • 25
  • 8

1 Answers1

0

You have to load the file into a null terminated char buffer first as specified here in the official documentation.

http://rapidxml.sourceforge.net/manual.html#classrapidxml_1_1xml__document_8338ce6042e7b04d5a42144fb446b69c_18338ce6042e7b04d5a42144fb446b69c

Just read the contents of your file into a char array and use this array to pass to the xml_document::parse() function.

If you are using ifstream, you can use something like the following to read the entire file contents to a buffer

 ifstream file ("test.xml");
 if (file.is_open())
 {
    file.seekg(0,ios::end);
    int size = file.tellg();
    file.seekg(0,ios::beg);

    char* buffer = new char [size];

    file.read (buffer, size);
    file.close();

    // your file should now be in the char buffer - 
    // use this to parse your xml     

    delete[] buffer;
 }

Please note I have not compiled the above code, just writing it from memory, but this is the rough idea. Look at the documentation for ifstream for exact details. This should help you get started anyway.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • Hey thanks a lot it worked. but not perfectly. some time it works fine and the buffer takes the input from file perfectly but other times it has some junk values at the end of the buffers and that is when the statement doc.parse<0>(buffer); fails and gives the following error on running not compilation. "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. terminate called after throwing an instance of 'rapidxml::parse_error' what(): expected <" please help – jj 1 Aug 05 '12 at 18:06
  • Yes you need to catch the exception from parsing. If your file has some unexpected junk in it, the parsing can throw the error. If you use a try/catch block to catch that error and deal with it. And dont forget that when you read the file into a char buffer you should make sure that the buffer is zero terminated like the documentation says. This means create the buffer to be one byte larger than file size, memset the whole buffer to zero, then read the file into the buffer. Of course if your xml files contain errors then there is nothing you can do - that is why the exception is thrown – mathematician1975 Aug 05 '12 at 19:09
  • thanks a lot mathematician1975. the problem was with the zero termination of the buffer. I set the last char to zero at the end and it worked. – jj 1 Aug 06 '12 at 10:33
  • @jj1 glad to help. if you havent already, take a good look at that documentation in the link above. there is a lot of useful stuff in it. – mathematician1975 Aug 06 '12 at 10:48