c++ - 增强属性树

标签 c++ xml boost-propertytree

基于此处的 boost 文档:

http://www.boost.org/doc/libs/1_41_0/doc/html/boost_propertytree/container.html

“一个节点中可能有多个具有相同键值的子节点。但是,这些子节点不一定是连续的。find返回的迭代器可能引用其中的任何一个,并且不能保证子节点的相对位置其他同名的 child 。”

示例 XML:

<library>
   <book><title>Title_1</title></book>
   <book><title>Title_2</title></book>
   <book><title>Title_3</title></book>
</library>

示例增强代码:

ptree pt;
pt.push_back(ptree::value_type("book", ptree("title")))

// This finds the first book and cannot iterate to the second one:
ptree::const_iterator it = pt.find("book");

知道了这一点,您将如何获得所有书籍并确保您将它们全部看完?

最佳答案

您必须使用equal_range函数:

std::pair < ptree::const_assoc_iterator, ptree::const_assoc_iterator> bounds = 
    pt.equal_range("book");

for (ptree::const_assoc_iterator it = bounds.first; it != bounds.second ; ++it)
{
    // process *it
}

关于c++ - 增强属性树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327032/

相关文章:

c++ - boost 属性 ptree : boost write_xml adding unicode 0x0 character in child element in xml file

c++ - QNX 错误 - 静态成员变量的 "Undefined reference to"

c++ - 为什么我的快速排序如此低效?

c++ - 模板类构造函数语法

c# - 在获取其值之前验证节点是否存在

javascript - 将 XML 解析为 HTML/JS

c++ - 具有通用获取/设置实现的类属性树数据结构的框架/库?

c++ - 为什么不能在编译时解决运行时多态性?

android - 从 xml(remote) Android 将数据导入到 sqlite