用boost解析嵌套的xml [英] Parsing nested xml with boost

查看:63
本文介绍了用boost解析嵌套的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要读取到程序中的xml,我已经看到很多示例,如何使用boost中的属性树读取xml,但是我无法使其适用于我拥有的嵌套xml:

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;
    }
}


}

推荐答案

所以,对于要迭代每个属性的每个摄像机,不是吗?

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

如果是,那么您需要使用equal_range()而不是get_child();.后者返回一个子项,前者返回一系列关联迭代器,这些迭代器指向具有给定键的所有子项.

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.

因此,使用get_child()获取document.chunk.cameras,使用equal_range('camera')获取所有摄像机,对于每个摄像机,都使用equal_range('xmlattr')遍历其属性.

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.

这篇关于用boost解析嵌套的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆