使用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 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0406数列求和
题目 解决代码及点评 这个题目,还是考察for循环的使用 以及数列规律,该数列的特点是第n个分子 = 第n-1个分子 + 第n-2个分子,分母也是此规律 而另外一个规律是第n个分子和第n- ...
- STL set的用法
要使用set需要事先引入一个头文件 #include<set> set是一个不允许重复的集合,如果要重复可以使用multiset. 1.set的插入[set.insert();],会返回P ...
- iOS/Xcode异常:reason = “The model used to open the store is incompatible with the one used to create the store”
reason=The model used to open the store is incompatible with the one used to create the store 出现上述异常 ...
- linux进程解析--进程切换
为了控制进程的执行,linux内核必须有能力挂起正在cpu上运行的进程,换入想要切换的进程,也就是恢复以前某个挂起的进程,这就是linux的进程切换. 1进程切换的时机 一般来说,进程切换都是发生在 ...
- ios inHouse 公布应用
一.明白几个概念 1.企业版IDP: 即iOS Development Enterprise Program.注意是$299/Year那种.并非$99/Year的那种 2.In House:是指企业内 ...
- 异常关闭MyEclipse 8.6后,不能重启
删掉这两个文件.
- C++ 自定义结构体和类 内存对齐
为什么要提出内存对齐? 比如这么一种处理器,它每次读写内存的时候都从某个8倍数的地址开始,一次读出或写入8个字节的数据,假如软件能保证double类型的数据都从8倍数地址开始,那么读或写一个doubl ...
- 数据库 isnull()、nvl()、ifnull() 使用
SqlServer 插入字段值 为空 设置默认值 isnull(val_1,val_2) 当val_1为 null 的时候 返回 val_2的值 Oracle nvl(val_1,val_2) 当va ...
- 【Tomcat】java.lang.OutOfMemoryError
1.Java heap space Heap size 昨天在服务器上面部署项目,之前服务器上面已经有一个项目的了. 然后我再部署一个,上去跑了之后,居然内存溢出了. 好吧,内存溢出就调一下,调了之后 ...
- tomcat环境变量的配置(网上摘,全部验证通过)
tomcat环境变量的配置 1.===> 进入bin目录下,双击startup.bat看是否报错.一般肯定会报. 2.===> 右键我的电脑===>高级===>环境变量 ...