30

First of all I shall say that I think I got how it should be done but my code will not compile any way I try. I based my assumption on this official example of empty ptree trick. There you can find next line:

  const ptree &settings = pt.get_child("settings", empty_ptree<ptree>());

Which shows that it is (or should be) possible to get subptree out from ptree.

So I assumed we could iterate thru ptree with something like BOOST_FOREACH in such manner:

BOOST_FOREACH(const boost::property_tree::ptree &v,
    config.get_child("servecies"))
{

}

But I get next error:

Error 1 error C2440: 'initializing' : cannot convert from 'std::pair<_Ty1,_Ty2>' to 'const boost::property_tree::ptree &'

or if I try

BOOST_FOREACH(boost::property_tree::ptree &v,
    config.get_child("servecies", boost::property_tree::empty_ptree<boost::property_tree::ptree>()))
{

}

I get:

Error 1 error C2039: 'empty_ptree' : is not a member of 'boost::property_tree'

So what shall I do: how to iterate thru Boost Ptree and get sub Ptrees?

Update: I also tried such code

    BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
    config.get_child("path.to.array_of_objects"))
{
    std::cout << "First data: " << v.first.data() << std::endl;
    boost::property_tree::ptree subtree = (boost::property_tree::ptree) v.second ;
    BOOST_FOREACH(boost::property_tree::ptree::value_type &vs,
        subtree)
    {
        std::cout << "Sub data: " << vs.first.data() << std::endl;
    }
}

This compiles, does not throw any exeptions but does not cout any Sub data, it just skeeps thru this cycle.

Update 2:

Hm... something probably went wrong in my xml - now I get correct results with that code.

Rella
  • 59,216
  • 102
  • 341
  • 614

3 Answers3

32

The property tree iterators point to pairs of the form (key, tree) of type ptree::value_type. The standard loop for iterating through the children of the node at path therefore looks like:

BOOST_FOREACH(const ptree::value_type &v, pt.get_child(path)) {
    // v.first is the name of the child.
    // v.second is the child tree.
}
antonakos
  • 7,983
  • 2
  • 29
  • 34
  • my problem is that I have some kind of array of objects. So I have for example { "servecie": { "module": [ "file", "Admin", "HR" ] } "servecie": { "module": [ "file", "Admin", "HR" ] } } and I need to get each object as seprate ptree. Is such thing possible? – Rella Jul 12 '11 at 00:07
  • 1
    @Kabumbus Yes. The `v.second` values will be the ptrees for the objects of the array. – antonakos Jul 12 '11 at 00:30
27

Using C++11, you can use the following to iterate through all the children of the node at path:

ptree children = pt.get_child(path);
for (const auto& kv : children) {
    // kv is of type ptree::value_type
    // kv.first is the name of the child
    // kv.second is the child tree
}
AlexG
  • 4,451
  • 5
  • 21
  • 43
Ela782
  • 4,543
  • 5
  • 46
  • 61
  • 1
    In my opinion, the better solution, but perhaps a little more to the example would've been good. – gauteh Apr 26 '14 at 20:23
8

I had the same problem with iterating trough JSON sub nodes

boost::property_tree::read_json(streamJSON, ptJSON);

If you have a structure like:

{
 playlists: [ {
   id: "1",
   x: "something"
   shows: [
    { val: "test" },
    { val: "test1" },
    { val: "test2" }
   ]
 },
 {
   id: "2"
   x: "else",
   shows: [
    { val: "test3" }
   ]
 }
 ]
}

You can iterate trough child nodes like this:

BOOST_FOREACH(boost::property_tree::ptree::value_type &playlist, ptJSON.get_child("playlists"))
{
    unsigned long uiPlaylistId = playlist.second.get<unsigned long>("id");
    BOOST_FOREACH(boost::property_tree::ptree::value_type &show, playlist.second.get_child("shows."))
    {
       std::string strVal = show.second.get<std::string>("val");
    }
}

I could not find anything about path selector "shows." to select sub array. (notice the dot at the end)

Some good documentation can be found here: http://kaalus.atspace.com/ptree/doc/index.html

Hope this helps someone.

Evalds Urtans
  • 5,160
  • 1
  • 36
  • 26