How to iterate a boost property tree? How to iterate a boost property tree? xml xml

How to iterate a boost property tree?


Here is what I came up with after much experimentation. I wanted to share it in the community because I couldn't find what I wanted. Everybody seemed to just post the answer from the boost docs, which I found to be insufficient. Anyhow:

#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/json_parser.hpp>#include <string>#include <iostream>using namespace std; using boost::property_tree::ptree; string indent(int level) {  string s;   for (int i=0; i<level; i++) s += "  ";  return s; } void printTree (ptree &pt, int level) {  if (pt.empty()) {    cerr << "\""<< pt.data()<< "\"";  }  else {    if (level) cerr << endl;     cerr << indent(level) << "{" << endl;         for (ptree::iterator pos = pt.begin(); pos != pt.end();) {      cerr << indent(level+1) << "\"" << pos->first << "\": ";       printTree(pos->second, level + 1);       ++pos;       if (pos != pt.end()) {        cerr << ",";       }      cerr << endl;    }    cerr << indent(level) << " }";       }  return; }int main(int, char*[]) {  // first, make a json file:  string tagfile = "testing2.pt";   ptree pt1;  pt1.put("object1.type","ASCII");    pt1.put("object2.type","INT64");    pt1.put("object3.type","DOUBLE");    pt1.put("object1.value","one");    pt1.put("object2.value","2");    pt1.put("object3.value","3.0");    write_json(tagfile, pt1);   ptree pt;  bool success = true;   try {      read_json(tagfile, pt);       printTree(pt, 0);       cerr << endl;   }catch(const json_parser_error &jpe){      //do error handling      success = false  }  return success; }

Here is the output:

rcook@rzbeast (blockbuster): a.out{  "object1":   {    "type": "ASCII",    "value": "one"   },  "object2":   {    "type": "INT64",    "value": "2"   },  "object3":   {    "type": "DOUBLE",    "value": "3.0"   } }rcook@rzbeast (blockbuster): cat testing2.pt {    "object1":    {        "type": "ASCII",        "value": "one"    },    "object2":    {        "type": "INT64",        "value": "2"    },    "object3":    {        "type": "DOUBLE",        "value": "3.0"    }}


BOOST_FOREACH is just a convenient way for iterating that can be done by iterator, begin() and end()

Your_tree_type::const_iterator end = tree.end();for (your_tree_type::const_iterator it = tree.begin(); it != end; ++it)    ...

And since C++11 it's:

for (auto& it: tree)    ...


I ran into this issue recently and found the answers incomplete for my need, so I came up with this short and sweet snippet:

using boost::property_tree::ptree;void parse_tree(const ptree& pt, std::string key){  std::string nkey;  if (!key.empty())  {    // The full-key/value pair for this node is    // key / pt.data()    // So do with it what you need    nkey = key + ".";  // More work is involved if you use a different path separator  }  ptree::const_iterator end = pt.end();  for (ptree::const_iterator it = pt.begin(); it != end; ++it)  {    parse_tree(it->second, nkey + it->first);  }}

Important to note is that any node, except the root node can contain data as well as child nodes. The if (!key.empty()) bit will get the data for all but the root node, we can also start building the path for the looping of the node's children if any.

You'd start the parsing by calling parse_tree(root_node, "") and of course you need to do something inside this function to make it worth doing.

If you are doing some parsing where you don't need the FULL path, simply remove the nkey variable and it's operations, and just pass it->first to the recursive function.