c++ - boost read_xml from stringstream does not read xml format -


I want to fill a boost :: property_tree :: ptree with data from XML, XML format is in string I The string passed through the stream and then I tried to read it with read_xml, but when I look at the object while debugging the PTT data is empty or empty, my code:

  std :: stringstream ss; SS & lt; & Lt; "& Lt;? Xml? & Gt;  & gt; Exam /> & lt; / root & gt;"; Boost :: property_tree :: ptree pt; Boost :: property_tree :: xml_parser :: read_xml (SS, PT);  

Result:

  pt {m_data = "" m_children = 0x001dd3b0}  

Before I get a string This XML code:

  & lt ;? XML version = "1.0"? & Gt; & Lt; Response Location = "910" Requested ID = "12" Serial Number = "0" & ​​gt; & Lt; Eid & gt; 1 & lt; / Id & gt; & Lt; Type & gt; P & lt; / Type & gt; & Lt; StatusMessage & gt; & Lt; / StatusMessage & gt; & Lt; Message & gt; Error & lt; / Messages & gt; & Lt; / Reaction & gt;  

But nothing works with C ++ using Visual Studio.

There is no data related to the root node, so m_data Empty, but a child node ( test ) and m_children! = Nullptr is .

Please consider this example:

  #include & lt; Sstream & gt; #include & lt; String & gt; # Include & lt; Boost / property_tree / xml_parser.hpp & gt; Int main () {std :: stringstream ss; SS & lt; & Lt; "& Lt;? Xml? & Gt;  & gt; Exam /> & lt; / root & gt;"; Boost :: property_tree :: ptree pt; Boost :: property_tree :: xml_parser :: read_xml (SS, PT); // There is no data related to the root node ... std :: string s (pt.get  ("root")); Std :: cout & lt; & Lt; "EXAMPLE1" & lt; & Lt; Std :: endl & lt; & Lt; "Data related to root node:" & lt; & Lt; S & lt; & Lt; Std :: endl; // ... but a child node is std :: cout & lt; & Lt; "Children of the root node:"; (Auto r: pt.get_child (for "root")) std :: cout & lt; & Lt; RFFT & Lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Std :: endl & lt; & Lt; Std :: endl; Std :: stringstream ss2; SS2 & Lt; & Lt; "& Lt ;? xml? & Gt;  Dummy & lt; / root & gt;"; Boost :: property_tree :: xml_parser :: read_xml (SS2, PT); // This time we have a string associated with the root string :: string s2 (pt.get and lt; std :: string & gt; ("root")); Std :: cout & lt; & Lt; "EXAMPLE2" & lt; & Lt; Std :: endl & lt; & Lt; "Data related to root node:" & lt; & Lt; S2 & lt; & Lt; Std :: endl; Return 0; }  

This will print:

  data associated with root route: EXAMPLE1 Root Nodes: Children of the Root Node: Examples EXAMPLE2 Data connected to the root route: Dummy  

().

The Boost Property Tree Library does not fully document its capabilities, but a good guide to parsing XML with Boost


Comments