Alex Rivera | Logout

Boost Property_Tree iterators, how to handle them?

Asked 2011-01-04T18:30:54.027
15

I am sorry, I asked a question about the same topic before, but my problem concerns another aspect of the one described there (How to iterate a boost...).

Take a look at the following code:

#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/algorithm/string/trim.hpp>
int main(int argc, char** argv) {
     using boost::property_tree::ptree;
     ptree pt;
     read_xml("try.xml", pt);
     ptree::const_iterator end = pt.end();
     for (ptree::const_iterator it = pt.begin(); it != end; it++)
           std::cout << "Here " << it->? << std::endl;
}

Well, as I got told told in the question I mentioned, there is the possibility to use iterators on property_tree in Boost, but I do not know what type it is, and what methods or properties I can use.

Well, I assume that it must be another ptree or something representing another xml hierarchy to be browsed again (if I want) but documentation about this is very bad. I do not know why, but in boost docs I cannot find nothing good, just something about a macro to browse nodes, but this approach is one I would really like to avoid.

So getting to my question here: Once getting the iterator on a ptree, how can I access node name, value, parameters (a node in a xml file)? Thankyou

Edit
Report

1 Answer

0

You should have the prior knowledge on the input property file.

Boost property tree is not a general document parser. It will do parsing and give access to the data, but must locate it manually.

I don't know there are method to navigate whole document, but if you only need the properties of your own file you can do it with very simple code.

From the boost documentation:

1) The throwing version (get):

ptree pt;
/* ... */
float v = pt.get<float>("a.path.to.float.value");

2) The default-value version (get):

ptree pt;
/* ... */
float v = pt.get("a.path.to.float.value", -1.f);

3) The optional version (get_optional):

ptree pt;
/* ... */
boost::optional<float> v = pt.get_optional<float>("a.path.to.float.value");
answered 2011-01-04T20:26:55.360

Your Answer