xml - how to make boost property tree print all subtrees and then end parent tree -
in code below iam trying traverse through nested map , not sure how property tree interpreting data.
it printing parent tree data , starts printing child tree
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <iostream> #include <fstream> #include <boost/archive/xml_oarchive.hpp> #include <string> #include <iostream> #include <map> #include <map> #include <iostream> #include <string> #include <vector> #include <set> #include <iterator> using namespace boost::property_tree; struct values { std::string a; std::string b; values():a("milepost"),b("dummyval"){}; values( std::string ab, std::string bc) { a=ab; b=bc; }; bool operator<(const values& other) const { return (a< other.a && b < other.b) ; } friend std::ostream& operator<<(std::ostream& os, const values& val); }; std::ostream& operator<< (std::ostream& os , const values& val) { os << val.a <<"\t"<< val.b; return os; } int main(int argc, const char * argv[]) { ptree pt; auto& list = pt.add_child("list", ptree{}); auto& assetlist = pt.add_child("assetlist", ptree{}); //create set of types firstly std::set<std::string> types ; types.insert("et"); types.insert("pt"); typedef std::map< int,std::set<std::string> > mygroupsmap; typedef std::map<std::string,values> mywsdata; typedef std::map< std::string , std::set<mywsdata> > myfinaldata; typedef std::map< int,std::set<myfinaldata> > mygroupdata; mygroupsmap grouptypemap; grouptypemap.insert(std::make_pair(1, types) ); grouptypemap.insert(std::make_pair(2, types) ); grouptypemap.insert(std::make_pair(3, types) ); grouptypemap.insert(std::make_pair(4, types) ); grouptypemap.insert(std::make_pair(5, types) ); //create object of structure values a,b,c; //create map , insert string , corresponding structure mywsdata et_data1,pt_data2; et_data1.insert(std::make_pair("780256", a)); et_data1.insert(std::make_pair("780255", b)); et_data1.insert(std::make_pair("780254", c)); pt_data2.insert(std::make_pair("780256", a)); pt_data2.insert(std::make_pair("780255", b)); pt_data2.insert(std::make_pair("780254", c)); std::set<mywsdata> myet_pt_data; myet_pt_data.insert(et_data1); myet_pt_data.insert(pt_data2); myfinaldata fd , fd1 , fd2 ; fd.insert(std::make_pair ("et",myet_pt_data) ); fd.insert(std::make_pair ("pt",myet_pt_data) ); fd1.insert(std::make_pair ("et",myet_pt_data) ); fd1.insert(std::make_pair ("pt",myet_pt_data) ); fd2.insert(std::make_pair ("et",myet_pt_data) ); fd2.insert(std::make_pair ("pt",myet_pt_data) ); std::set<myfinaldata> mfd; mfd.insert(fd); mfd.insert(fd1); mfd.insert(fd2); mygroupdata mygd; mygd.insert(std::make_pair (1,mfd) ); mygd.insert(std::make_pair (2,mfd) ); mygd.insert(std::make_pair (3,mfd) ); (auto &a : mygd) { for(auto &b:a.second) //get std::set { for(auto &the_map:b) //get map set { list.add("groupid", a.first); list.add("grouptype",the_map.first); for(auto &the_set:the_map.second) { // the_map takes values set // the_map actual type std::map<std::string,values>& (const auto& the_map : the_set) { // the_value takes value of current map (the_map) // the_value actual type std::pair<std::string,values>& assetlist.add("assetid",the_map.first); assetlist.add("milepost",the_map.second.a); assetlist.add("dummy",the_map.second.b); // list.add("value", map.first) // .add("<xmlattr>.active", true); } } } } } xml_parser::write_xml(std::cout, pt); return 0; }
output(excerpt):
<?xml version="1.0" encoding="utf-8"?> <list> <groupid>1</groupid> <grouptype>et</grouptype> <grouptype>pt</grouptype> <groupid>2</groupid> <grouptype>et</grouptype> <grouptype>pt</grouptype> <groupid>3</groupid> <grouptype>et</grouptype> <grouptype>pt</grouptype> </list> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid><milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist>
intended output(excerpt)
<groupid>1</groupid> <grouptype>et</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid><milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> <grouptype>pt</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid><milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> <groupid>2</groupid> <grouptype>et</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid><milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> <grouptype>pt</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid><milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist>
how add in systematic way ? code can run @ http://coliru.stacked-crooked.com/a/763741c78d494654
thanks tejas
trees contain subtrees. printing parent tree is printing subtrees before closing parent.
both current output , "intended" output invalid documents (there can 1 root element).
you'll need put data single tree, e.g.:
ptree pt; auto& root = pt.add_child("root", ptree{}); auto& list = root.add_child("list", ptree{}); auto& assetlist = root.add_child("assetlist", ptree{});
which prints following document:
<?xml version="1.0" encoding="utf-8"?> <root> <list> <groupid>1</groupid> <grouptype>et</grouptype> <groupid>1</groupid> <grouptype>pt</grouptype> <groupid>2</groupid> <grouptype>et</grouptype> <groupid>2</groupid> <grouptype>pt</grouptype> <groupid>3</groupid> <grouptype>et</grouptype> <groupid>3</groupid> <grouptype>pt</grouptype> </list> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </root>
which well-formed xml :)
update comments
if want group asset lists grouptypes, .... need write code that?
ptree pt; auto& list = pt.add_child("list", ptree{}); //create set of types firstly std::set<std::string> types { "et", "pt" }; typedef std::map<std::string,values> mywsdata; typedef std::map< std::string , std::set<mywsdata> > myfinaldata; typedef std::map< int,std::set<myfinaldata> > mygroupdata; values a,b,c; mywsdata et_data1 { { "780256", }, { "780255", b }, { "780254", c } }, pt_data2 = et_data1; std::set<mywsdata> myet_pt_data { et_data1, pt_data2 }; myfinaldata fd , fd1 = fd, fd2 = fd; std::set<myfinaldata> mfd { { { "et", myet_pt_data }, { "pt", myet_pt_data } }, { { "et", myet_pt_data }, { "pt", myet_pt_data } }, { { "et", myet_pt_data }, { "pt", myet_pt_data } } }; mfd.insert(fd); mfd.insert(fd1); mfd.insert(fd2); mygroupdata mygd; mygd.insert(std::make_pair (1,mfd) ); mygd.insert(std::make_pair (2,mfd) ); mygd.insert(std::make_pair (3,mfd) ); (auto &a : mygd) { for(auto &b:a.second) //get std::set { for(auto &the_map:b) //get map set { auto& group = list.add_child("group", {}); group.add("<xmlattr>.groupid", a.first); group.add("grouptype",the_map.first); auto& assetlist = group.add_child("assetlist", ptree{}); for(auto &the_set:the_map.second) { // the_map takes values set // the_map actual type std::map<std::string,values>& (const auto& the_map : the_set) { // the_value takes value of current map (the_map) // the_value actual type std::pair<std::string,values>& assetlist.add("assetid",the_map.first); assetlist.add("milepost",the_map.second.a); assetlist.add("dummy",the_map.second.b); // assetlist.add("value", map.first) // .add("<xmlattr>.active", true); } } } } } xml_parser::write_xml(std::cout, pt);
prints:
<?xml version="1.0" encoding="utf-8"?> <list> <group groupid="1"> <grouptype>et</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> <group groupid="1"> <grouptype>pt</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> <group groupid="2"> <grouptype>et</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> <group groupid="2"> <grouptype>pt</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> <group groupid="3"> <grouptype>et</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> <group groupid="3"> <grouptype>pt</grouptype> <assetlist> <assetid>780254</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780255</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> <assetid>780256</assetid> <milepost>milepost</milepost> <dummy>dummyval</dummy> </assetlist> </group> </list>
Comments
Post a Comment