使用Boost property tree来解析带attribute的xml

转自:https://blog.csdn.net/mosaic/article/details/6888934

boost property tree的5分钟教程虽然简单明了,可惜使用的xml不够典型。今天由于工作上要读取带属性并且有几层嵌套的xml配置文件,因此研究了一下如何使用。下面直接列出测试用的xml文件内容和程序代码。


debug_settings.xml,在boost的例子上改得稍微复杂一些。

[html]  view plain  copy
  1. <debug name="debugname">  
  2.     <file name="debug.log"/>  
  3.     <modules type="internal">  
  4.         <module1>Finance_Internal</module1>  
  5.         <module2>Admin_Internal</module2>  
  6.         <module3>HR_Internal</module3>  
  7.     </modules>  
  8.       
  9.     <modules type="external">  
  10.         <module>Finance_External</module>  
  11.         <module>Admin_External</module>  
  12.         <module>HR_External</module>    
  13.     </modules>  
  14. </debug>  


Source code:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <boost/property_tree/ptree.hpp>  
  4. #include <boost/property_tree/xml_parser.hpp>  
  5. #include <boost/foreach.hpp>  
  6.   
  7. using namespace std;  
  8. using namespace boost::property_tree;  
  9.   
  10. int main(void){  
  11.     ptree pt;  
  12.     read_xml("debug_settings.xml", pt);  
  13.   
  14.         //loop for every node under debug  
  15.     BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug")){  
  16.   
  17.         if(v1.first == "<xmlattr>"){ //it's an attribute  
  18.             //read debug name="debugname"  
  19.             cout<< "debug name=" << v1.second.get<string>("name") << endl;  
  20.         }else if(v1.first == "file"){  
  21.             //read file name="debug.log"  
  22.             cout << "  file name=" << v1.second.get<string>("<xmlattr>.name") << endl;  
  23.         }  
  24.         else// v1.first == "modules"  
  25.             //get module type  
  26.             cout<< "  module type:" << v1.second.get<string>("<xmlattr>.type") << endl;  
  27.   
  28.             //loop for every node under modules  
  29.             BOOST_FOREACH(ptree::value_type &v2, v1.second){  
  30.                 if(v2.first == "<xmlattr>"){  //it's an attribute  
  31.                     //this can also get module type  
  32.                     cout<< "  module type again:" << v2.second.get<string>("type") << endl;  
  33.                 }  
  34.                 else{  
  35.                     //all the modules have the same structure, so just use data() function.  
  36.                     cout<< "    module name:" << v2.second.data() << endl;  
  37.                 }  
  38.             }//end BOOST_FOREACH  
  39.         }  
  40.     }//end BOOST_FOREACH  
  41. }  

程序输出如下:

debug name=debugname
  file name=debug.log
  module type:internal
  module type again:internal
    module name:Finance_Internal
    module name:Admin_Internal
    module name:HR_Internal
  module type:external
  module type again:external
    module name:Finance_External
    module name:Admin_External
    module name:HR_External


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值