Boost属性树:如何使用xml文件获取子树的子树 [英] Boost property tree : how to get child of child tree with a xml file
问题描述
我尝试使用Boost库(1.57.0.2版)解析一个简单的xml文件,并尝试访问链接字段值.
I try to parse a simple xml file using Boost library (version 1.57.0.2) and I try to access to link field values.
请在我的xml文件下面找到:
Please find, here below my xml file :
<?xml version="1.0" encoding="UTF-8"?>
<down>
<food name="cream">
<link name="123" />
<link name="456" />
</food>
<tel name="ice">
<link name="78" />
<link name="90" />
</tel>
</down>
我尝试查看如何从网络中获取孩子并做同样的事情,但是我不明白为什么它不能与我的代码一起使用.
I try to see how to get child of child from web and do the same but I don't understand why it doesn't work with my code.
我遇到以下错误:
error: 'const value_type {aka const struct std::pair<const std::__cxx11::basic_string<char>, boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >}' has no member named 'get_child'|
请在下面找到我的代码:
Please find my code here below :
boost::property_tree::xml_parser::read_xml(path.xml, tree);
BOOST_FOREACH(const boost::property_tree::ptree::value_type &child_tree, tree.get_child("down"))
{
std::string tree_balise = tree.first;
if (tree_balise == "food")
{
BOOST_FOREACH(const boost::property_tree::ptree::value_type &child_of_child_tree, child_tree.get_child("food"))
{
std::cout << "I am in the child of child tree" << std::endl;
}
}
}
有人可以告诉我为什么会出现此错误吗? 我想念什么吗?
Does somebody could tell me why this error appears ? Am I missing something ?
谢谢您的帮助.
Miwa.
推荐答案
-
实际上
boost::property_tree::ptree::value_type
是key_type
的pair
(在您的情况下为std::string
)和ptree
.因此,在第二个FOREACH
中,您必须使用child_tree.second
,并且错误指出pair
没有get_child
成员函数.
Actually
boost::property_tree::ptree::value_type
is apair
ofkey_type
(std::string
in your case) andptree
. So in 2ndFOREACH
you have to usechild_tree.second
and error says thatpair
has noget_child
member function.
在第二个FOREACH
范围内,您已经在"food"子树中.因此它没有食物"孩子.
In 2nd FOREACH
scope you are already in "food" subtree. So it doesn't have "food" child.
BOOST_FOREACH( const ptree::value_type & child_tree, tree.get_child("down") )
{
std::string tree_balise = child_tree.first;
if (tree_balise == "food")
{
BOOST_FOREACH( const ptree::value_type & child_of_child_tree, child_tree.second )
{
std::cout << "I am in the child of child tree " << child_of_child_tree.first << std::endl;
}
}
}
还请注意,您可以直接获取"down.food"子树.
Also Note that you can directly get "down.food" subtree.
BOOST_FOREACH( const ptree::value_type & child_tree, tree.get_child("down.food") )
{
std::cout << "I am in the child of child tree " << child_tree.first << std::endl;
}
这篇关于Boost属性树:如何使用xml文件获取子树的子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!