Skip to content

Instantly share code, notes, and snippets.

@nekko1119
Last active December 30, 2015 17:49
Show Gist options
  • Save nekko1119/7863658 to your computer and use it in GitHub Desktop.
Save nekko1119/7863658 to your computer and use it in GitHub Desktop.
boost::property_tree::ptreeで、形式の定まっていないxmlファイル等の全ての要素を再帰的に得る方法。
<debug>
<filename>debug.log</filename>
<modules>
<module>Finance</module>
<module>Admin</module>
<module>HR</module>
</modules>
<level>2</level>
</debug>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <iostream>
using boost::property_tree::ptree;
void show_all(ptree const& pt, int indent = 0) {
for (auto const& it : pt) {
for (int i = 0; i < indent; ++i) {
std::cout << "\t";
}
std::cout << "<" << it.first << ">";
if (it.second.empty()) {
std::cout << it.second.data();
} else {
std::cout << std::endl;
show_all(it.second, indent + 1);
for (int i = 0; i < indent; ++i) {
std::cout << "\t";
}
}
std::cout << "</" << it.first << ">" << std::endl;
}
}
int main() {
ptree pt;
read_xml("debug.xml", pt);
show_all(pt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment