c++ - 使用 boost 解析嵌套的 xml

标签 c++ xml boost

我有一个 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;
    }
}


}

最佳答案

所以,对于每个相机,您想要遍历每个属性,不是吗?

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

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

关于c++ - 使用 boost 解析嵌套的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784849/

相关文章:

c++ - 如何在 C++ 中不使用 cin 扫描字符串?

C++ 右值引用转发性能

xml - XSLT 将所有子元素包装在父标记中

c# - 使用不带属性的 DataContractSerializer 或 XmlSerializer 删除所有命名空间

c++ - 正则表达式替换:到 ":"等

c++ - 将 char 绑定(bind)到枚举类型

c++ - OpenCV:基于三角选择倾斜图像

xml - XSLT 2.0 - 正则表达式无限

c++ - boost::make_shared 现在过时了吗?

c++ - 使用 boost::assign 和嵌套在 std::map 中的 std::set