The second BOOST_FOREACH is calling g.get_child("path")
which returns the first child of g
called path
. You need to iterate over all of the children with the key that you are looking for.
This can be achieved using:
/** Find the range of children that have the given key. */
std::pair<assoc_iterator, assoc_iterator>
ptree::equal_range(const key_type &key);
/** Count the number of direct children with the given key. */
size_type count(const key_type &key) const;
e.g.
typedef boost::property_tree::ptree::value_type vt;
BOOST_FOREACH (vt const &nodes, pt.get_child("svg"))
{
//only action on g's and not desc, comments, etc.
if(nodes.first=="g")
{
const boost::property_tree::ptree& g = nodes.second;
//only action on g's that contain paths, not g->rect, for example.
if(g.count("path") != 0)
{
std::pair< ptree::const_assoc_iterator,
ptree::const_assoc_iterator > bounds = g.equal_range( "path" );
for( ptree::const_assoc_iterator it = bounds.first; it != bounds.second; ++it )
{
std::cout << it->first << " : ";
const ptree& d = it->second.get_child( "<xmlattr>.d" );
std::cout << d.get_value<std::string>() << "\n";
}
}
}
}
I find the following code useful when working with ptree, it will display your entire parsed ptree:
void display_ptree(ptree const& pt)
{
BOOST_FOREACH( const ptree::value_type& v, pt )
{e
std::cout << v.first << ": " << v.second.get_value<std::string>() << "\n";
display_ptree( v.second );
}
}