// 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");
}
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");
}
The other answers are non-ideal, because using istringstream needlessly copies the entire buffer.
As an answer on this question suggests, you could use the deprecated istrstream, but as this generates warnings and may be removed in future, a better solution is to use boost::iostreams:
This avoids needlessly copying the buffer in the same way istrstream did (which can be a considerable problem, if your input buffer is large), and saves you having to write your own stream class.
发布评论
评论(3)
这是一些对我有用的代码......
Heres some code that works for me...
将字符串包装在
istringstream
中。Wrap the string in an
istringstream
.其他答案并不理想,因为使用 istringstream 不必要地复制整个缓冲区。
正如这个问题的答案所示,您可以使用已弃用的
istrstream
,但由于这会生成警告并可能在将来被删除,更好的解决方案是使用 boost::iostreams:这可以避免像
istrstream
那样不必要地复制缓冲区(如果您的输入缓冲区很大,这可能是一个相当大的问题) ,并且使您不必编写自己的流类。The other answers are non-ideal, because using
istringstream
needlessly copies the entire buffer.As an answer on this question suggests, you could use the deprecated
istrstream
, but as this generates warnings and may be removed in future, a better solution is to use boost::iostreams:This avoids needlessly copying the buffer in the same way
istrstream
did (which can be a considerable problem, if your input buffer is large), and saves you having to write your own stream class.