2017-10-17 85 views
0

我有一個XML,我喜歡讀入我的程序,我見過很多exmaples如何閱讀XML使用屬性樹從提升,但是我不能讓它適用於嵌套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; 
    } 
} 


} 

回答

0

所以,對於每個相機你想迭代每個屬性,不是嗎?

如果是,那麼你需要使用equal_range()而不是get_child();後者返回一個單獨的孩子,前者是一系列聯合迭代器,指向具有給定密鑰的所有孩子。

因此,使用get_child()來獲得document.chunk.cameras,使用equal_range(「相機」),以獲得所有相機和爲每個攝像機使用equal_range(「xmlattr」)遍歷其屬性。

+0

謝謝!它解決了我的問題 – user3178756