Rel*_*lla 17 c++ xml boost input boost-propertytree
Boost有一个关于如何从文件加载XML的教程.如何使用我在代码中创建或从用户接收的字符串(例如with cin)来提供它?
小智 12
下面是一些适合我的代码......
// 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");
}
Run Code Online (Sandbox Code Playgroud)
其他答案是不理想的,因为使用会istringstream不必要地复制整个缓冲区。
就像这个问题的答案所建议的那样,您可以使用deprecated istrstream,但是由于这会生成警告并且将来可能会删除,因此更好的解决方案是使用boost :: iostreams:
boost::iostreams::stream<boost::iostreams::array_source> stream(moo.c_str(), moo.size());
boost::property_tree::read_json(stream, tree);
Run Code Online (Sandbox Code Playgroud)
这样可以避免不必要地以相同的方式复制缓冲区istrstream(如果输入缓冲区很大,这可能是一个大问题),并且省去了编写自己的流类的麻烦。
| 归档时间: |
|
| 查看次数: |
9736 次 |
| 最近记录: |