很抱歉,我之前问过一个关于同一主题的问题,但我的问题涉及那里描述的另一个方面 (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/