Note that there are some explanatory texts on larger screens.

plurals
  1. POReading XML data using boost::property_tree library functions in C++
    primarykey
    data
    text
    <pre><code>&lt;?xml version="1.0"?&gt; &lt;sked&gt; &lt;version&gt;2&lt;/version&gt; &lt;flight xmlns:xsi="some_uri" xsi:type="emirates"&gt; &lt;carrier&gt;BA&lt;/carrier&gt; &lt;number&gt;4001&lt;/number&gt; &lt;date&gt;2011-07-21&lt;/date&gt; &lt;/flight&gt; &lt;flight xmlns:xsi="some_uri" xsi:type="cathey-pacific"&gt; &lt;flight_class&gt; &lt;type&gt;Economy&lt;/type&gt; &lt;fare&gt;400&lt;/fare&gt; &lt;/flight_class&gt; &lt;date&gt;2011-07-21&lt;/date&gt; &lt;/flight&gt; &lt;/sked&gt; </code></pre> <p>I have a XML document which describe 2 types of flights by same keywords. Sub fields are depend on the type of the flight. I have to read the XML and store it into C++ data classes according to the type of flights. </p> <p>This is my code segment that is used for this purpose.</p> <pre><code>typedef boost::property_tree::ptree Node; Node pt; read_xml(test.xml, pt); Node skedNode = pt.get_child("sked"); Node flightNode = skedNode.get_child("flight"); BOOST_FOREACH(Node::value_type const&amp; v, skedNode.get_child("sked")) { if (v.first == "flight") { if (v.second.get("&lt;xmlattr&gt;.xsi:type", "Null") == "cathey-pacific") { BOOST_FOREACH(Node::value_type const&amp; v1, flightNode.get_child("flight")) { if(v1.first == "flight_class") FlightClass fclass = FlightClass(static_cast&lt;Node&gt;(flightNode)); } } } } </code></pre> <p>When I try to run the above code, I got nothing inside <code>FlightClass</code>. I tried to debug the above code and found, <code>v1.first</code> is getting the values "carrier", "number" and "value" only. I surprised because, those are the parameter of emirates type of flights. I couldn't receive cathey-pacific flight information. Please help me to find out what the issue is. I really want to get the information of cathy-pacific flights from this XML file and store into C++ data classes. What should I do to correct this?</p> <p>Note: Instead of second <code>BOOST_FOREACH</code>, I tried <code>v.second.get_child("flight");</code> but it's throwing an exception. Then I replaced above by <code>v.second.get_child("flight_class");</code> and it's giving it's sub-fields like: type and fare. What may be the reason for that? It seems it's returning its grandchild nodes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload