boost读取xml

来源:互联网 发布:眼部干纹 知乎 编辑:程序博客网 时间:2024/05/22 05:21

1. include

#include <boost/property_tree/ptree.hpp>  #include <boost/property_tree/xml_parser.hpp>  #include <boost/foreach.hpp> using namespace boost::property_tree; 

2. Source code

bool load(){ptree pt;  try{read_xml("CPublishCommonCfg.xml",pt);     ptree child = pt.get_child("Publish");  BOOST_FOREACH(ptree::value_type &service, child){  ServiceInfoPtr service_info(new ServiceInfo());service_info->service_name = service.first.data();std::map<std::string,std::string> map;if (!service.second.empty()) {BOOST_FOREACH(ptree::value_type &model, service.second){std::string type = model.first.data();transform(type.begin(),type.end(),type.begin(), ::tolower);<span style="white-space:pre"></span>if(type == "in"){service_info->in_queue = model.second.data();}else if(type == "model"){std::string name;std::string out;if(!model.second.empty()){BOOST_FOREACH(ptree::value_type &leaf, model.second){std::string type = leaf.first.data();transform(type.begin(),type.end(),type.begin(), ::tolower);if(type == "name"){name = leaf.second.data();}else if(type == "out"){out = leaf.second.data();}else{LOGGER_ERROR("Leaf type config error: must be model");}}if(!name.empty()&&!out.empty()){service_info->out_queue_map[name] = out;}}} }}else{LOGGER_WARN("service node can't be a leaf");}service_conf_.push_back(service_info);}  }catch (std::exception const&  ex){LOGGER_ERROR("Can't init settings: "<<ex.what());}return true;}

3.xml

<Publish>    <Service>      <In>CQ.SDBUS.CSwap.PublishQueue.Quote</In>      <Model1>        <Name>A</Name>        <Out>QUEUE1</Out>         </Model1>      <Model2>         <Name>B</Name>        <Out>QUEUE2</Out>      </Model2>      <Model3>        <Name>C</Name>        <Out>QUEUE3</Out>      </Model3>   </Service><Publish>


0 0