【问题标题】:iterate over boost::property_tree XML迭代 boost::property_tree XML
【发布时间】:2012-10-15 19:06:06
【问题描述】:

如果我对设置结构一无所知,如何迭代 boost::property_tree(XML 部分)?

我想知道所有字段的名称和 xmlattr 名称。

我在这个库的 boost 文档中没有看到这样的例子。

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    查看提升文档here。在页面的下半部分,您可以看到以下示例代码:

    // Iterate over the debug.modules section and store all found
    // modules in the m_modules set. The get_child() function
    // returns a reference to the child at the specified path; if
    // there is no such child, it throws. Property tree iterators
    // are models of BidirectionalIterator.
    BOOST_FOREACH(ptree::value_type &v,
            pt.get_child("debug.modules"))
        m_modules.insert(v.second.data());
    

    只需修改代码以遍历根属性树中的所有模块(如果您愿意,还可以递归到子树中)。

    例如:

    ptree my_tree;
    // Read in your XML to the tree
    ...
    BOOST_FOREACH(ptree::value_type &v,
         pt, do_something_with_field(v.second.data());
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 2011-06-03
      • 2023-03-07
      • 2013-03-03
      • 1970-01-01
      • 2013-04-14
      • 2018-04-11
      相关资源
      最近更新 更多