tags:

views:

63

answers:

2

I'm parsing an XML file which is quite simple:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with XML.</description>
   </book>
</catalog>

And when I parse it using the default settings, i.e parse<0>(), like so:

    ifstream file(xmlFile.c_str(),  ifstream::in );
int length;
file.seekg (0, ios::end);
length = (int)file.tellg();
file.seekg (0, ios::beg);
char xmlBuf[ length + 1 ];
file.read( &xmlBuf[0], length );
xmlBuf[length] = '\0';

document.parse<0>(xmlBuf);

All the nodes are in the correct places and have the correct lengths when queried using xml_node.value_size() or xml_node.name_size(), but actually getting the name or value into a string or even just printf(), it returns a lot of stuff like the following:

(........./.(..............-.   <titlK.

Has anyone else run into this?

+1  A: 

If you are using C++, why not do it the C++ way?

int main(int argc, char* argv[]) 
{
  std::string buf;
  std::string line;
  std::ifstream in("file.xml");

  // read file into buf
  while(std::getline(in,line))
    buf += line;

  // After that, take a look on the traverse_xml() function implemented
  // here: http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/
  // It has great stuff!

  return 0;
}
karlphillip
A: 

Looks like my problem was just changing the parse declaration to:

document.parse<parse_full>();

Thanks to Karl for pointing me to http://www.ffuts.org/blog/quick-notes-on-how-to-use-rapidxml/, that's an excellent resource.

Joshua Noble