【发布时间】:2015-03-22 19:27:50
【问题描述】:
以下是我想用Boost.PropertyTree 库处理的实际 xml 的简化示例。实际上,原始 xml 文件中还有很多其他字段
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar>
<item>
<link>http://www.one.com</link>
</item>
<item>
<link>http://www.two.net</link>
</item>
<item>
<link>http://www.sex.gov</link>
</item>
...
</bar>
</foo>
我需要遍历所有link 标签。有一个所需代码的示例。
for (auto item: pt.get_child("foo.bar"))
if ("item" == item.first)
for (auto prop: item.second)
if ("link" == prop.first)
std::cout << prop.second.get_value<std::string>() << std::endl;
这对于简单的目的来说太丑陋了。 有没有办法简化它?例如,我可以等待下一个代码有效:
for (auto item: pt.get_child("foo.bar.item.link"))
std::cout << item.second.get_value<std::string>() << std::endl;
此代码不起作用,但它说明了我想要得到什么。
【问题讨论】:
标签: c++ boost boost-propertytree