boost::property_tree实例用法

本文介绍了如何使用boost::property_tree库解析XML和JSON文件,通过示例展示了读取XML配置文件并获取数据的过程,以及如何将数据存储为Property Tree并输出为XML和JSON格式。
摘要由CSDN通过智能技术生成

boost::property_tree 的解析xml模块是基于rapidXml的, 以下是两个例子:

例子1: 

[cpp]  view plain copy
  1. #include <boost/property_tree/ptree.hpp>  
  2. #include <boost/property_tree/xml_parser.hpp>  
  3. #include <boost/typeof/typeof.hpp>  
  4. #include <iostream>  
  5.   
  6. using namespace std;  
  7.   
  8. void ReadConfig()  
  9. {  
  10.   boost::property_tree::ptree pt;  
  11.         boost::property_tree::read_xml("del.conf", pt);  
  12.         filenum = pt.get<int>("root.delfile.filenum");  
  13.   
  14.         cout << "filenum: " << filenum << endl;  
  15.           
  16.         BOOST_AUTO(child, pt.get_child("root.delfile.paths"));  
  17.         for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)  
  18.         {  
  19.          BOOST_AUTO(child_paths, pos->second.get_child("")); //此处不需要填结点名,但引号不能省.  
  20.          for (BOOST_AUTO(pos_paths, child_paths.begin()); pos_paths != child_paths.end(); ++pos_paths)  
  21.           cout << pos_paths->second.data() << endl;  
  22.         }  
  23.           
  24. }  
  25. int main()  
  26. {  
  27.  ReadConfig();  
  28.  return 0;  
  29. }  
  30.   
  31.   
  32. /* 
  33. 附录:配置文件del.conf 
  34. */  

[html]  view plain copy
  1. <root>  
  2.  <delfile>  
  3.   
  4.   <filenum> 35 </filenum>  
  5.   
  6.   <paths>  
  7.    <path>   
  8.     <pathname>/tmp/tmp0/</pathname>  
  9.     <before_hours> 0 </before_hours>  
  10.    </path>  
  11.     
  12.    <path>   
  13.     <pathname>/tmp/tmp1/</pathname>  
  14.     <before_hours> 1 </before_hours>  
  15.    </path>  
  16.     
  17.    <
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值