On Aug 21, 2010, at 9:01 AM, Dietmar Hummel wrote:
void load( const std::string& filename ) { boost::property_tree::ptree pt; boost::property_tree::read_xml(filename, pt); BOOST_FOREACH( boost::property_tree::ptree::value_type &v, pt.get_child( "a.b.c" ) ) { parseCNode( v ); } }
----
Now I have several questions for this: 1. The code doesn't work as expected, it just handles the first one of the c nodes
get_child returns one node with the given path. If the path leads to several nodes, which one is chosen is unspecified. Worse, because of the way paths are resolved, you might get no child even though there is one that fits the path. If, for example, you have a second b element that doesn't contain any c elements, and that b element is chosen for a.b, then a.b.c would return nothing. In short, ptree paths aren't xpath, and do not work very well when you have dupes.
2. The function parseCNode() also doesn't work. I assumed that v is a reference to the c node where I have to claim the d-node by get_child. But it seems v is already pointing to d?
pt.get_child doesn't return a collection of nodes, it returns a ptree. That ptree provides an STL interface for its child nodes, the value type being the name-node pair. Thus, your for_each loop is fed one of the c nodes as the collection, so it iterates over the children of that node, which are d nodes. What you really want is to first call get_child("a.b") to get the b node, then call equal_range("c") on it to get all the c nodes, and iterate over that. Sebastian