2

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.

Miwa
  • 45
  • 1
  • 1
  • 5

1 Answers1

3
  1. 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.

  2. 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;
}
JustRufus
  • 492
  • 1
  • 5
  • 10
  • Could I ask you something else ? – Miwa Apr 06 '17 at 08:45
  • I try to access to my link attributs values with the following line : std::string value = child_tree.second.get(boost:propertu_tree::xml_parser::xmlattr() + ".name") but I crash when I run my executable. I think I'm not use correctly child_tree members functions, isn't it ? – Miwa Apr 06 '17 at 08:48
  • @Miwa you can use `child_tree.second.get(".name")`. But make sure that you are on `` subtree and not in food's name attribute. `if ( child_tree.first != "" ) { std::cout << "I am in the child of child tree " << child_tree.second.get(".name") << std::endl; }` – JustRufus Apr 06 '17 at 09:24
  • yes I am sure I am on , I have the following code : 'code'BOOST_FOREACH(const boost::property_tree::ptree::value_type &child_of_child_tree, child_tree.second) { std::string link_balise = child_of_child_tree.first ; std::string iei_name; if (link_balise != "") { std::cout << "I am in the child of child tree " << std::endl; std::string iei_name = child_of_the_child.second.get (boost::property_tree::xml_parser::xmlattr() + ".name"); }'code' It is the same as yours, isn't it ? – Miwa Apr 06 '17 at 09:38
  • Oh, I just replace get(boost::property_tree::xml_parser::xmlattr() + ".name"); by get(".name") and it works.... why ?... – Miwa Apr 06 '17 at 09:46
  • @Miwa I am not sure what `boost::property_tree::xml_parser::xmlattr()` is. you can read docs – JustRufus Apr 06 '17 at 09:47
  • @Miwa https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – JustRufus Apr 06 '17 at 10:29