2011-11-14 69 views
2

我加載一個boost :: property_tree :: ptree中從一個XML文件,這看起來有點像這樣:如何遍歷boost :: propertytree中的子節點,如果這些子節點本身?

<bla> 
    <foo> 
     <element id="1" type="..." path="..."/> 
     <element id="2" type="..." path="..."/> 
     <element id="3" type="..." path="..."/> 
     <otherelement/> 
    </foo> 
</bla> 

我加載到屬性樹與read_xml。現在我想構建一個包含結構的向量,類似於element -tags。我能做到以下幾點:

BOOST_FOREACH(ptree::value_type& node, tree.get_child("bla.foo")) 
{ 
    if (node.first == "element") 
    { 
       ... 
    } 
} 

到目前爲止,這是很好的,但我有問題獲得元素中的數據。 node.second應該包含,但我如何正確訪問它? node.second.get("xmlattr.type")不起作用。

回答

0

以正確的方式使用Google-Fu後,我在this blogentry中找到了自己的答案。正確的方法是node.second.get<std::string>("<xmlattr>.type")。除了我錯誤的xmlattr-thing外,避免編譯器錯誤的方法是使用模板語法。

相關問題