[关闭]
@zhengyuhong 2017-02-08T08:55:16.000000Z 字数 962 阅读 1169

boost

boost


boost/property_tree

  1. #include <boost/property_tree/ptree.hpp>
  2. #include <boost/property_tree/xml_parser.hpp>
  3. int load_xml(const std::string& filename) {
  4. boost::property_tree::ptree pt;
  5. boost::property_tree::read_xml(filename, pt);
  6. if (pt.find("version") != pt.not_found()) {
  7. printf("version[%s]\n", pt.get<std::string>("version").c_str()); //get函数提供默认值
  8. }
  9. std::string key = "sofa"; //key 支持多层节点路径,如sofa.runtime
  10. boost::property_tree::ptree::const_assoc_iterator iter = pt.find(key);
  11. if (iter != pt.not_found()) {
  12. const auto& subpt = pt.get_child(key); //key支持多层节点路径。如sofa.runtime
  13. if (subpt.find("<xmlattr>") != subpt.not_found()) {
  14. const auto& attr = subpt.get_child("<xmlattr>");
  15. std::string imp = attr.get<std::string>("imp");
  16. printf("attr[imp], value[%s]\n", imp.c_str());
  17. }
  18. for (auto iter = subpt.begin(); iter != subpt.end(); ++iter) {
  19. std::string key = iter->first;
  20. std::string value = iter->second.get_value<std::string>();//get_value提供默认值
  21. if (key != "<xmlattr>") {
  22. printf("key[%s], value[%s]\n", key.c_str(), value.c_str());
  23. }
  24. }
  25. }
  26. return 0;
  27. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注