Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost PropertyTree: check if child exists

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?

like image 381
paul23 Avatar asked Sep 27 '11 11:09

paul23


2 Answers

optional< const ptree& > child = node.get_child_optional( "possibly_missing_node" ); if( !child ) {   // child node is missing } 
like image 191
RobH Avatar answered Sep 19 '22 23:09

RobH


Here's a couple of other alternatives:

if( node.count("possibliy_missing") == 0 ) {    ... }  ptree::const_assoc_iterator it = ptree.find("possibly_missing"); if( it == ptree.not_found() ) {    ... } 
like image 22
Michael Anderson Avatar answered Sep 19 '22 23:09

Michael Anderson