【问题标题】:Parsing nested xml with boost使用 boost 解析嵌套的 xml
【发布时间】:2017-10-17 07:35:38
【问题描述】:

我有一个我喜欢读入我的程序的 xml,我已经看到很多例子如何使用 boost 中的属性树来读取 xml,但是我无法让它适用于我拥有的嵌套 xml:

<?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>

我只对读取相机节点及其属性感兴趣,为此我使用了以下代码,但它不起作用:

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


}

【问题讨论】:

    标签: c++ xml boost


    【解决方案1】:

    所以,对于每个相机,您要迭代每个属性,不是吗?

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

    所以,使用 get_child() 获取 document.chunk.cameras,使用 equal_range('camera') 获取所有相机,并为每个相机使用 equal_range('xmlattr') 遍历其属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多