带有属性树的boost xml c ++简单解析 [英] c++ simple parse with boost xml with property tree
本文介绍了带有属性树的boost xml c ++简单解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对boost xml解析有这个问题:
I have this question about the boost xml parsing:
这是我的Xml片段:
<Clients>
<Client name="Alfred" />
<Client name="Thomas" />
<Client name="Mark" />
</Clients>
然后我用以下代码读取名称:
and I read the name with this code:
std::string name = pt.get<std::string>("Clients.Client.<xmlattr>.name, "No name");
并且工作正常,但始终检索第一个节点.
and works fine, but retrieve always the first node..
有没有办法获得第二个,第三个节点而不循环?
Is there a way to get the second, third node without looping?
谢谢
推荐答案
在属性树中无法查询多值键. (部分原因是大多数受支持的后端格式都不正式支持重复密钥.)
There's no facility to query multi-valued keys in Property Tree. (Partly because most of the supported backend formats do not officially support duplicate keys).
但是,您可以遍历子元素,因此可以实现自己的查询,如下所示:
However, you can iterate through child elements, so you can implement your own query, like so:
for (auto& child : pt.get_child("Clients"))
if (child.first == "Client")
std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n";
请参阅完整的示例 在Coliru上直播 :
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <iostream>
using boost::property_tree::ptree;
int main()
{
std::stringstream ss("<Clients>\n"
" <Client name=\"Alfred\" />\n"
" <Client name=\"Thomas\" />\n"
" <Client name=\"Mark\" />\n"
"</Clients>");
ptree pt;
boost::property_tree::read_xml(ss, pt);
for (auto& child : pt.get_child("Clients"))
{
if (child.first == "Client")
std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n";
}
};
这篇关于带有属性树的boost xml c ++简单解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文