1

I have an xml which I like to read into my program, I've seen a lot of exmaples how to read xml using property tree from boost, however I couldn't make it work for the nested xml that I have:

<?xml version="1.0" encoding="UTF-8"?>
   <document version="1.3.0">
      <chunk>
         <sensors>
               <sensor id="0" label="unknown" type="frame">
                    <resolution width="2056" height="2464"/>
                    <property name="fixed" value="0"/>
                    <calibration type="frame" class="adjusted">
                          <resolution width="2056" height="2464"/>
                          <fx>7349.85579147491</fx>
                          <fy>7349.85579147491</fy>
                          <cx>1028</cx>
                          <cy>1232</cy>
                          <p1>0.000308132854297239</p1>
                          <p2>-0.000521332855614243</p2>
                    </calibration>
               </sensor>
         </sensors>
         <cameras>
               <camera id="0" label="img0000.png" sensor_id="0" enabled="1">
                    <transform>1.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 -1.0000000000000000e+000 -1.2246467991473532e-016 0.0000000000000000e+000 0.0000000000000000e+000 1.2246467991473532e-016 -1.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 1.0000000000000000e+000</transform>
                </camera>
                <camera id="1" label="img0011.png" sensor_id="0" enabled="1">
                     <transform>9.8183676675341047e-001 -2.7892274662900951e-002 -1.8766615162393202e-001 1.3502780959894856e+000 -2.8076662610258110e-002 -9.9960436765543659e-001 1.6760611099915072e-003 -8.8020303958543274e-003 -1.8763865398120155e-001 3.6234208013954891e-003 -9.8223144235654503e-001 -1.1015316085201440e-001 0.0000000000000000e+000 0.0000000000000000e+000 0.0000000000000000e+000 1.0000000000000000e+000</transform>
               </camera>
      </cameras>
      <reference>LOCAL_CS["Local Coordinates (m)",LOCAL_DATUM["Local Datum",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]]]</reference>
     <region>
        <center>-5.1216167685514069e-002 -7.0600760442627708e-001 -6.9904047240845895e+000</center>
        <size>2.1074647950629393e+000 1.5533586459855240e+000 1.0253878730038792e+000</size>
        <R>-9.6011547075389880e-001 2.7340714563038887e-001 5.8539008680217816e-002 -2.6221584379471408e-001 -9.5313222127937347e-001 1.5093647677772853e-001 9.7062526662174770e-002 1.2956659089939432e-001 9.8680867671533157e-001</R>
    </region>
    <settings>
       <property name="accuracy_tiepoints" value="1"/>
       <property name="accuracy_cameras" value="10"/>
       <property name="accuracy_cameras_ypr" value="2"/>
       <property name="accuracy_markers" value="0.005"/>
       <property name="accuracy_scalebars" value="0.001"/>
       <property name="accuracy_projections" value="0.1"/>
     </settings>
   </chunk>
</document>

I'm only interested in reading the cameras node and their attributes, and I've used the following code for that, but it doesn't work:

using namespace std;
using namespace boost;
using namespace boost::property_tree;

const ptree& empty_ptree() {
    static ptree t;
    return t;
}

int main()
{
ptree tree;
read_xml(XML_PATH1, tree);
tree = tree.get_child("document.chunk", empty_ptree());
const ptree & formats = tree.get_child("cameras.camera", empty_ptree());
BOOST_FOREACH(const ptree::value_type & f, formats)
{
    string at = f.first + ".<xmlattr>";
    const ptree & attributes = f.second.get_child("<xmlattr>", empty_ptree());
    cout << "Extracting attributes from " << at << ":" << endl;
    BOOST_FOREACH(const ptree::value_type &v, attributes) 
    {
        cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
    }
}


}

1 Answer 1

1

so, for each camera you want to iterate over each attribute, don't you ?

if yes, then you need to use equal_range() instead of get_child(); the latter returns a single child, the former a range of associative iterators pointing to all childs with the given key.

so, use get_child() to get document.chunk.cameras, use equal_range('camera') to get all cameras and for each camera use equal_range('xmlattr') to traverse its attributes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.