Reputation: 45
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.
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.
I have the following error :
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 ?
Thank you for your help.
Miwa.
Upvotes: 2
Views: 12069
Reputation: 542
Actually boost::property_tree::ptree::value_type
is a pair
of key_type
(std::string
in your case) and ptree
. So in 2nd FOREACH
you have to use child_tree.second
and error says that pair
has no get_child
member function.
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;
}
}
}
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;
}
Upvotes: 3