c++ Boost循环通过未知的ptree和列表属性值

C++ Boost loop through unknown ptree and list values of attributes

本文关键字:ptree 列表 属性 未知 Boost 循环 c++      更新时间:2023-10-16

我想解析包含特定属性的节点的随机xml文件,并检索该属性的所有值。用例是有许多具有不同节点的xml,但是要检索的属性总是已知的。

下面是一个文件示例:

<node>
 <container>
  <object attribute="value" />
  <object attribute="value" />
 <container/>
 <supercontainer>
  <subcontainer>
   <otherobject attribute="value" />
  <subcontainer/>
 <supercontainer/>
</node>

这是我现在使用boost property_tree的内容,但我不知道在循环中该怎么做:

ptree pt;
read_xml(xml_file, pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &ele, pt)
{
   //NO IDEA
}

想法欢迎。

谢谢

好的,我用下面的方法解决了这个问题:

void recurseManifest(ptree &pt, int lvl)
{
  for (ptree::iterator current = pt.begin(); current != pt.end();)
  {
    try
    {
        std::cout << current->second.get<std::string>("<xmlattr>.attribute") << std::endl;
    }
    catch(const boost::property_tree::ptree_bad_path &err)
    {
        std::cerr << err.what() << std::endl;    
    }
    assets = recurseManifest(current->second, lvl++);
    ++current;
  }
}