使用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 ...
随机推荐
- 文字适应DIV
今天突然碰到了一个奇怪的问题 那就是对于纯数字和英文字母 文字多了会超出div 且即使是设置了height:auto overflow-y:auto 也不管用 只是在x轴上出现滚动条 不论用 ...
- 在 Azure 网站上使用 Memcached 改进 WordPress
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. 您是否希望改善在 ...
- BZOJ 1828
program bzoj1828; ; check=; type node=record l,r,s,a:longint; end; ..maxn*] of node; a,b,c:..maxn] o ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- Android 中 ListView Adapter getView 被多次调用问题 解决方法
执行多次原因是因为每显示一个VIew,它都去测量view的高度,执行measure方法,导致getView执行多次. 解决方法是将 ListView 的 layout_width 设置为 fill_p ...
- Java--再次理解多态
Java中多态性(polymorphism)的实现 什么是多态 1. 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. ...
- 设置QPushButton的平面与突出(遍历控件)
#include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...
- 如何抓取Thread Dump小结(转)
当系统性能出现问题时,需要从各个方面来查看网络环境.主机资源.查看最经变更的代码等.如果是想从代码层面解决问题,那么最有效的方法就是查看相关dump文件.如果是使用IBM JDK(我默认你是在aix环 ...
- zk mysql 主从自动切换
zookeeper测试: DBI 版本: /DBI-1.616# zjtest7-redis:/root/DBD-mysql-4.031# perl Makefile.PL Can't exec &q ...
- 一年四个P(Project)
盼望着,盼望着,提高班众多革命同胞的假期终于来了.伴随着校园之中越来越多的同学身影,暑假学习时的那份静谧一散而去,恍然间在提高班学习的第二个年头也已经过去了(~_~),而自己的大学生涯也就像秋后的蚂蚱 ...