2014-02-18 1 views
1

XML에서 데이터를 boost :: property_tree :: ptree로 채우고 싶습니다. xml 형식이 stringstream에 전달 된 문자열에있는 다음 시도합니다. 내 코드, read_xml와 함께 읽을 수 있지만 내가 개체 디버깅하는 동안 볼 때 ptree 데이터는 null 또는 비어 :stringstream에서 부스트 read_xml이 xml 형식을 읽지 않습니다

std::stringstream ss; 
ss << "<?xml ?><root><test /></root>"; 
boost::property_tree::ptree pt; 
boost::property_tree::xml_parser::read_xml(ss, pt); 

결과 :이 XML을 문자열을 가지고 전에

pt {m_data="" m_children=0x001dd3b0 } 

코드 :

<?xml version="1.0"?><Response Location="910" RequesterId="12" SequenceNumber="0"> 
<Id>1</Id> 
<Type>P</Type> 
<StatusMessage></StatusMessage> 
<Message>Error</Message> 
</Response> 

그러나 Visual Studio를 사용하여 C++로는 아무 것도 작동하지 않습니다.

답변

2

루트 노드와 관련된 데이터가 없으므로 m_data이 비어 있지만 자식 노드 (테스트)와 m_children != nullptr이 있습니다.

이 예제를 고려하십시오 :

#include <sstream> 
#include <string> 
#include <boost/property_tree/xml_parser.hpp> 

int main() 
{ 
    std::stringstream ss; 
    ss << "<?xml ?><root><test /></root>"; 
    boost::property_tree::ptree pt; 
    boost::property_tree::xml_parser::read_xml(ss, pt); 

    // There is no data associated with root node... 
    std::string s(pt.get<std::string>("root")); 
    std::cout << "EXAMPLE1" << std::endl << "Data associated with root node: " << s << std::endl; 

    // ...but there is a child node. 
    std::cout << "Children of root node: "; 
    for (auto r : pt.get_child("root")) 
    std::cout << r.first << std::endl; 

    std::cout << std::endl << std::endl; 

    std::stringstream ss2; 
    ss2 << "<?xml ?><root>dummy</root>"; 
    boost::property_tree::xml_parser::read_xml(ss2, pt); 

    // This time we have a string associated with root node 
    std::string s2(pt.get<std::string>("root")); 
    std::cout << "EXAMPLE2" << std::endl << "Data associated with root node: " << s2 << std::endl; 

    return 0; 
} 

그것은 인쇄 할 수 있습니다 :

EXAMPLE1 
Data associated with root node: 
Children of root node: test 

EXAMPLE2 
Data associated with root node: dummy 

(http://coliru.stacked-crooked.com/a/34a99abb0aca78f2을).

Boost 속성 트리 라이브러리는 기능을 완벽하게 문서화하지는 않지만 부스트를 사용하여 XML을 구문 분석하는 데 유용한 지침은 http://akrzemi1.wordpress.com/2011/07/13/parsing-xml-with-boost/

입니다.
관련 문제