如何在boost :: propertytree中迭代子节点,如果它们本身有子节点?

时间:2011-11-14 11:42:14

标签: xml boost-propertytree

我从XML文件加载了boost :: property_tree :: ptree,看起来有点像这样:

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

我将它加载到带有read_xml的属性树中。现在我想构建一个包含结构的向量,类似于element - 标签。我可以做到以下几点:

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

到目前为止它很好,但是我在获取元素中的数据时遇到了问题。 node.second应包含该内容,但如何正确访问? node.second.get("xmlattr.type")不起作用。

1 个答案:

答案 0 :(得分:0)

以正确的方式使用Google-Fu后,我在this blogentry找到了答案。正确的方法是node.second.get<std::string>("<xmlattr>.type")。除非我得到xmlattr-thing错误,否则避免编译错误的方法是使用模板语法。