Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 448
Rella Avatar asked Mar 13 '11 11:03

Rella


2 Answers

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");
}
like image 172
jugs Avatar answered Oct 06 '22 00:10

jugs


Wrap the string in an istringstream.

like image 20
Fred Foo Avatar answered Oct 05 '22 23:10

Fred Foo