2

我从 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-tags。我可以执行以下操作:

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

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

4

1 回答 1

0

在正确使用 Google-Fu 之后,我自己在这篇博文中找到了答案。正确的方法是node.second.get<std::string>("<xmlattr>.type")。除了我弄错了 xmlattr-thing,避免我得到的编译器错误的方法是使用模板语法。

于 2011-11-14T12:25:37.797 回答