부스트는 tutorial on how to load XML from a file입니다. 코드에서 생성하거나 사용자로부터 수신하는 문자열 (예 : cin
)을 피드에 어떻게 제공합니까?Boost.PropertyTree에 파일이 아닌 문자열을 입력하는 방법은 무엇입니까?
14
A
답변
10
istringstream
에 문자열을 감싸십시오. 나를 위해 작동
11
Heres는 몇 가지 코드 ... istringstream
불필요 복사 전체 버퍼를 사용하기 때문에
// 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");
}
4
다른 답변, 비 이상적입니다.
는 this question에 대한 대답은 당신이 사용되지 않는 istrstream
를 사용할 수 있듯이,하지만이 경고를 생성 미래에서 제거 될 수 있으므로, 더 나은 솔루션 boost::iostreams 사용하는 것입니다 : 이것은 불필요하게 복사 피한다
boost::iostreams::stream<boost::iostreams::array_source> stream(moo.c_str(), moo.size());
boost::property_tree::read_json(stream, tree);
같은 방식으로 istrstream
(입력 버퍼가 클 경우 상당한 문제가 될 수 있음)과 동일한 방식으로 버퍼를 만들고 사용자 자신의 스트림 클래스를 작성하지 않아도됩니다.