c++ - Boost Property_Tree 迭代器,如何处理它们?

标签 c++ iterator boost-propertytree

很抱歉,我之前问过一个关于同一主题的问题,但我的问题涉及那里描述的另一个方面 (How to iterate a boost...)。

看看下面的代码:

#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/algorithm/string/trim.hpp>
int main(int argc, char** argv) {
     using boost::property_tree::ptree;
     ptree pt;
     read_xml("try.xml", pt);
     ptree::const_iterator end = pt.end();
     for (ptree::const_iterator it = pt.begin(); it != end; it++)
           std::cout << "Here " << it->? << std::endl;
}

好吧,正如我在我提到的问题中被告知的那样,有可能在 Boost 中的 property_tree 上使用迭代器,但我不知道它是什么类型,以及什么方法或属性我可以使用。

好吧,我假设它必须是另一个 ptree 或代表另一个要再次浏览的 xml 层次结构的东西(如果我愿意的话),但有关此的文档非常糟糕。我不知道为什么,但是在 boost 文档中我找不到任何好的东西,只是关于浏览节点的宏,但这种方法是我非常想避免的。

所以在这里解决我的问题:一旦在 ptree 上获得迭代器,我如何访问节点名称、值、参数(xml 文件中的节点)? 谢谢你

最佳答案

打印完整的树:

void print(boost::property_tree::ptree const& pt)
{
    using boost::property_tree::ptree;
    ptree::const_iterator end = pt.end();
    for (ptree::const_iterator it = pt.begin(); it != end; ++it) {
        std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
        print(it->second);
    }
}

关于c++ - Boost Property_Tree 迭代器,如何处理它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597048/

相关文章:

c++ - 需要帮助理解指针算法

c++ - 如何在 map 中使用结构指针?

c++ - 我该如何解决此错误 : . exe 文件已停止工作

c++ - double 或浮点比较

c++ - 在模板类上 boost property_tree : multiple values per key,

C++: boost ptree remove children: 没有匹配函数

python - 如何实现自定义迭代器以便可以嵌套它们?

c++ - 如何使用迭代器在 C++ 中的递归函数中传递值?

python - 使用 clint 进度条显示 urllib.urlretrieve() 的状态

c++ - 对于 Boost.Propertytree,有什么方法可以使用 JSON 点符号来引用数组元素?