使用boost中的property_tree实现配置文件
<debug>
<total>3</total>
<persons>
<person>
<age>23</age>
<name>hugo</name>
</person>
<person>
<age>23</age>
<name>mayer</name>
</person>
<person>
<age>30</age>
<name>boy</name>
</person>
</persons>
</debug>
1 struct person
2 {
3 int age;
4 std::string name;
5 };
6
7 struct debug_persons
8 {
9 int itsTotalNumber;
10 std::vector<person> itsPersons;
11 void load(const std::string& filename);
12 void save(const std::string& filename);
13 };
1 void debug_persons::save( const std::string& filename )
2 {
3 using boost::property_tree::ptree;
4 ptree pt;
5
6 pt.put("debug.total", itsTotalNumber);
7
8 BOOST_FOREACH(const person& p,itsPersons)
9 {
10 ptree child;
11 child.put("age",p.age);
12 child.put("name",p.name);
13 pt.add_child("debug.persons.person",child);
14 }
15 // Write property tree to XML file
16 write_xml(filename, pt);
17
18 }
19
20 void debug_persons::load( const std::string& filename )
21 {
22 using boost::property_tree::ptree;
23 ptree pt;
24
25 read_xml(filename, pt);
26 itsTotalNumber = pt.get<int>("debug.total");
27
28 BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.persons"))
29 {
30 //m_modules.insert(v.second.data());
31 person p;
32 p.age = v.second.get<int>("age");
33 p.name = v.second.get<std::string>("name");
34 itsPersons.push_back(p);
35 }
36 }
1 int _tmain(int argc, _TCHAR* argv[])
2 {
3
4 try
5 {
6 debug_persons dp;
7 dp.load("debug_persons.xml");
8 std::cout<<dp<<std::endl;
9 person p;
10 p.age = 100;
11 p.name = "old man";
12 dp.itsPersons.push_back(p);
13 dp.save("new.xml");
14 std::cout << "Success\n";
15 }
16 catch (std::exception &e)
17 {
18 std::cout << "Error: " << e.what() << "\n";
19 }
20 return 0;
21 }
1 std::ostream& operator<<(std::ostream& o,const debug_persons& dp)
2 {
3 o << "totoal:" << dp.itsTotalNumber << "\n";
4 o << "persons\n";
5 BOOST_FOREACH(const person& p,dp.itsPersons)
6 {
7 o << "\tperson: Age:" << p.age << " Nmae:" << p.name << "\n";
8 }
9 return o;
10 }
1 #include <boost/property_tree/ptree.hpp>
2 #include <boost/property_tree/xml_parser.hpp>
3 #include <boost/foreach.hpp>
4 #include <vector>
5 #include <string>
6 #include <exception>
7 #include <iostream>
使用boost中的property_tree实现配置文件的更多相关文章
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- spring项目中dubbo相关的配置文件出现红叉的问题
近来在eclipse中导入了一个web项目,但是发现项目上有红色的叉号. 原来是spring中关于dubbo的配置文件报错了. Multiple annotations found at this l ...
- .NET平台开源项目速览(20)Newlife.Core中简单灵活的配置文件
记得5年前开始拼命翻读X组件的源码,特别是XCode,但对Newlife.Core 的东西了解很少,最多只是会用用,而且用到的只是九牛一毛.里面好用的东西太多了. 最近一年时间,零零散散又学了很多,也 ...
- git上传中的排除的配置文件, git实际的操作代码;
git上传中的排除的配置文件: git实际的操作 在主目录建立.gitignore文件并输入以下保存: *.class #package file *.war *.ear #kdiff3 ignore ...
- Linux故障:linux中使用ifconfig命令查看网卡信息时显示为eth1,但是在network-scripts中只有ifcfg-eth0的配置文件,并且里面的NAME="eth0"。
linux中使用ifconfig命令查看网卡信息时显示为eth1,但是在network-scripts中只有ifcfg-eth0的配置文件,并且里面的NAME="eth0". ...
- vue-cli脚手架build目录中的build.js配置文件
该随笔收藏自: vue-cli脚手架build目录中的build.js配置文件 这个配置文件是命令npm run build 的入口配置文件,主要用于生产环境 由于这是一个系统的配置文件,将涉及很多的 ...
- Boost中的智能指针(转)
这篇文章主要介绍 boost中的智能指针的使用.(转自:http://www.cnblogs.com/sld666666/archive/2010/12/16/1908265.html) 内存管理是一 ...
- nginx指令中的优化(配置文件)
nginx指令中的优化(配置文件)worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 00000001 0000 ...
- Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...
随机推荐
- 下载的firebug-lite压缩包的调用方法
把以下代码copy到地址栏按回车,等加载完毕之后(受网速限制,有时候等待会久一点,如果很久都无法加载,重试几次就ok了),就会显示firebug lite的窗口,这样不用更改页面任何东西,任何地方轻松 ...
- c++ 复制构造函数和赋值函数
c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...
- ZSTU OJ 3999 零基础学算法---邻接表
题目:Click here 题意:我就喜欢中文题! 分析:这个题虽然是中文题,但是还是有一点费解的.其实就是给你一棵树,是用图的形式给你的,只知道a,b之间有一条边,并不知道谁是父,谁是子.思路就是先 ...
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )
线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...
- oracle常用函数以及调用入参为record的存储过程的方法,
转自:http://www.cnblogs.com/zhangronghua/archive/2007/08/20/862812.html SQL中的单记录函数1.ASCII返回与指定的字符对应的十进 ...
- 四级流水线的8bit加法器
以流水线实现8bit 加法器. //date : 2013/8/23 //designer :pengxiaoen //function : module pipeline ( clock ,rese ...
- Oracle中奇怪的【不等于号】
Oracle中奇怪的[不等于号] 在Oracle中,不等号有三种:<>,!=,^= 例如: select * from test where name<>'xn'.返回的结 ...
- 使用DiskGenius对虚拟机磁盘进行压缩
使用虚拟机的用户是否感觉到您的虚拟磁盘文件越来越大,都快把宝贵的磁盘空间(宿主机物理硬盘)占满了呢? 有人会想到,我直接启动虚拟机,然后把里面没用的数据删除了,不就行了吗?实际测试发现,这样删除后,存 ...
- c语言指针数组与数组指针
一.指针数组和数组指针的内存布局初学者总是分不出指针数组与数组指针的区别.其实很好理解:指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身决定.它是“储存指针的数组”的简称.数 ...
- UVa 121 - Pipe Fitters
称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...