关闭

boost读取xml

193人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41066次
    • 积分:1692
    • 等级:
    • 排名:千里之外
    • 原创:133篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条
    文章分类