How to feed Boost.PropertyTree with a string, not a file?

Rella picture Rella · Mar 13, 2011 · Viewed 11k times · Source

Boost has a tutorial on how to load XML from a file. How do I feed it with a string that I either create in code or receive from a user (e.g. with cin)?

Answer

jugs picture jugs · Dec 16, 2011

Heres some code that works for me...

// Create an empty property tree object
ptree xmlTree;

// Read the XML config string into the property tree. Catch any exception
try {
  stringstream ss; ss << xmlConfigString;
  read_xml(ss, xmlTree);
}
catch (xml_parser_error &e) {
  LOGERROR ("Failed to read config xml " << e.what());
}
catch (...) {
  LOGERROR ("Failed to read config xml with unknown error");
}