Last active
December 30, 2015 17:49
-
-
Save nekko1119/7863658 to your computer and use it in GitHub Desktop.
boost::property_tree::ptreeで、形式の定まっていないxmlファイル等の全ての要素を再帰的に得る方法。
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
<debug> | |
<filename>debug.log</filename> | |
<modules> | |
<module>Finance</module> | |
<module>Admin</module> | |
<module>HR</module> | |
</modules> | |
<level>2</level> | |
</debug> |
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 <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