Question

I've loaded an xml document from a website. I then try to parse it with pugixml.

  ParseXml::parseTrainsXml(char source[]) {    
  __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [ %s ]", source);
  size_t size = sizeof(source);

  pugi::xml_document doc;

  pugi::xml_parse_result result = doc.load_buffer(source, size);

  __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [ %s ]",
                result.description());
}

When I try to load it I get the following error: "Error parsing document declaration/processing instruction". So that didn't gave me alot of help so I tried to look it up. http://pugixml.googlecode.com/svn/tags/latest/docs/manual/loading.html

status_bad_pi means that parsing stopped due to incorrect document declaration/processing instruction

So basically, I got no idea why it's not working. When I declare the same xml like this:

const char source[] = "my xml here";

It does work if I declare it like that. Anyone know what could've gone wrong?

Was it helpful?

Solution

  size_t size = sizeof(source);

This line is incorrect. source is treated as a pointer to char, so sizeof is always 4 (or 8) => you only get the first 4/8 bytes parsed. Since you're passing an incomplete buffer to pugixml (likely "<?xm"), pugixml flags it as incorrect.

You have to pass the correct buffer size to your function, or, if the buffer is guaranteed to be null-terminated (not sure how you get the buffer), you can use doc.load(source) instead of doc.load_buffer(source, size).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top