@zhengyuhong
2017-02-08T08:55:16.000000Z
字数 962
阅读 1169
boost
#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/xml_parser.hpp>int load_xml(const std::string& filename) {boost::property_tree::ptree pt;boost::property_tree::read_xml(filename, pt);if (pt.find("version") != pt.not_found()) {printf("version[%s]\n", pt.get<std::string>("version").c_str()); //get函数提供默认值}std::string key = "sofa"; //key 支持多层节点路径,如sofa.runtimeboost::property_tree::ptree::const_assoc_iterator iter = pt.find(key);if (iter != pt.not_found()) {const auto& subpt = pt.get_child(key); //key支持多层节点路径。如sofa.runtimeif (subpt.find("<xmlattr>") != subpt.not_found()) {const auto& attr = subpt.get_child("<xmlattr>");std::string imp = attr.get<std::string>("imp");printf("attr[imp], value[%s]\n", imp.c_str());}for (auto iter = subpt.begin(); iter != subpt.end(); ++iter) {std::string key = iter->first;std::string value = iter->second.get_value<std::string>();//get_value提供默认值if (key != "<xmlattr>") {printf("key[%s], value[%s]\n", key.c_str(), value.c_str());}}}return 0;}
