Boost PropertyTree: check if child exists

paul23 picture paul23 · Sep 27, 2011 · Viewed 48.1k times · Source

I'm trying to write an XML parser, parsing the XML file to a boost::property_tree and came upon this problem. How can I check (quickly) if a child of a certain property exists?

Obviously I could iterate over all children using BOOST_FOREACH - however, isn't there a better solution to this?

Answer

RobH picture RobH · Sep 27, 2011
optional< const ptree& > child = node.get_child_optional( "possibly_missing_node" );
if( !child )
{
  // child node is missing
}