Last active
August 29, 2015 14:15
-
-
Save gruzovator/bcecf774244428be9169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdexcept> | |
#include <map> | |
#include <boost/property_tree/ptree.hpp> | |
#include <boost/property_tree/xml_parser.hpp> | |
using namespace std; | |
using namespace boost::property_tree; | |
int main(int argc, char const *argv[]) | |
try | |
{ | |
multimap<string, string> records = { | |
{"a", "a1"}, | |
{"a", "a2"}, | |
{"b", "b1"}, | |
{"a", "a3"}, | |
{"", "empty"}, | |
{"c", "c1"}, | |
{"", "empty"} | |
}; | |
{ // variant 1 | |
ptree p; | |
for(const auto& item: records) | |
{ | |
const auto& key = item.first; | |
const auto& value = item.second; | |
if(!key.empty()) | |
{ | |
p.put(key+"."+value, "smth"); | |
} | |
} | |
xml_writer_settings<char> settings('\t', 1); | |
xml_parser::write_xml(cout, p, settings); | |
} | |
{ // variant 2 | |
ptree p; | |
ptree subTree; | |
string currentKey; | |
for(const auto& item: records) | |
{ | |
const auto& key = item.first; | |
const auto& value = item.second; | |
if(key.empty()) continue; | |
if(key!=currentKey) | |
{ | |
if(!subTree.empty()) | |
p.add_child(currentKey, subTree); | |
subTree = ptree(); | |
currentKey = key; | |
} | |
subTree.put(value, "smth"); | |
} | |
if(!subTree.empty()) | |
p.add_child(currentKey, subTree); | |
xml_writer_settings<char> settings('\t', 1); | |
xml_parser::write_xml(cout, p, settings); | |
} | |
return 0; | |
} | |
catch(const exception& ex) { | |
cerr << "Exception: " << ex.what() << endl; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment