boost操作xml 5分钟官方教程
This tutorial uses XML. Note that the library is not specifically bound to XML, and any other supported format (such as INI or JSON) could be used instead. XML was chosen because the author thinks that wide range of people is familiar with it.
Suppose we are writing a logging system for some application, and need to read log configuration from a file when the program starts. The file with the log configuration looks like this:
<debug>
<filename>debug.log</filename>
<modules>
<module>Finance</module>
<module>Admin</module>
<module>HR</module>
</modules>
<level>2</level>
</debug>
It contains the log filename, a list of modules where logging is enabled, and the debug level value. To store the logging configuration in the program we created a debug_settings structure:
struct debug_settings
{
std::string m_file; // log filename
int m_level; // debug level
std::set<string> m_modules; // modules where logging is enabled
void load(const std::string &filename);
void save(const std::string &filename);
};
All that needs to be done now is to write implementations of load() and save() member functions. Let's first deal with load(). It contains just 7 lines of code, although it does all the necessary things, including error reporting:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp> // Loads debug_settings structure from the specified XML file
void debug_settings::load(const std::string &filename)
{
// Create an empty property tree object
using boost::property_tree::ptree
;
ptree
pt; // Load the XML file into the property tree. If reading fails
// (cannot open file, parse error), an exception is thrown.
read_xml
(filename, pt); // Get the filename and store it in the m_file variable.
// Note that we construct the path to the value by separating
// the individual keys with dots. If dots appear in the keys,
// a path type with a different separator can be used.
// If the debug.filename key is not found, an exception is thrown.
m_file = pt.get
<std::string>("debug.filename"); // Get the debug level and store it in the m_level variable.
// This is another version of the get method: if the value is
// not found, the default value (specified by the second
// parameter) is returned instead. The type of the value
// extracted is determined by the type of the second parameter,
// so we can simply write get(...) instead of get<int>(...).
m_level = pt.get
("debug.level", 0); // Iterate over the debug.modules section and store all found
// modules in the m_modules set. The get_child() function
// returns a reference to the child at the specified path; if
// there is no such child, it throws. Property tree iterators
// are models of BidirectionalIterator.
BOOST_FOREACH(ptree
::value_type
&v,
pt.get_child
("debug.modules"))
m_modules.insert
(v.second.data());
}
Now the save() function. It is also 7 lines of code:
// Saves the debug_settings structure to the specified XML file
void debug_settings::save(const std::string &filename)
{
// Create an empty property tree object
using boost::property_tree::ptree
;
ptree
pt; // Put log filename in property tree
pt.put
("debug.filename", m_file); // Put debug level in property tree
pt.put
("debug.level", m_level); // Iterate over the modules in the set and put them in the
// property tree. Note that the put function places the new
// key at the end of the list of keys. This is fine most of
// the time. If you want to place an item at some other place
// (i.e. at the front or somewhere in the middle), this can
// be achieved using a combination of the insert and put_own
// functions.
BOOST_FOREACH(const std::string &name, m_modules)
pt.add
("debug.modules.module", name); // Write the property tree to the XML file.
write_xml
(filename, pt);
}
The full program debug_settings.cpp is included in the examples directory.
from:http://www.boost.org/doc/libs/1_56_0/doc/html/boost_propertytree/tutorial.html
boost操作xml 5分钟官方教程的更多相关文章
- Dom4j完整教程,操作XML教程
目录 1.DOM4J简介 2.XML文档操作1 2.1.读取XML文档: 2.2.获取根节点 2.3.. 新增一个节点以及其下的子节点与数据 2.4. 写入XML文件 2. 5. 遍历xml节点 2. ...
- Spring 官方教程:使用 Restdocs 创建 API 文档
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247483998&idx=1&sn=6ae5fa795d36b1 ...
- 详解 “Android UI”设计官方教程
我们曾经给大家一个<MeeGo移动终端设备开发UI设计基础教程>,同时很多朋友都在寻找Android UI开发的教程,我们从Android的官方开发者博客找了一份幻灯片,介绍了一些Andr ...
- 正则表达式30分钟入门教程<转载>
来园子之前写的一篇正则表达式教程,部分翻译自codeproject的The 30 Minute Regex Tutorial. 由于评论里有过长的URL,所以本页排版比较混乱,推荐你到原处查看,看完了 ...
- DroidParts 中文系列教程(基于官方教程)
DroidParts中文系列教程(基于官方教程) (一)DroidParts框架概况 2014年4月18日星期五 11:36 他是一个精心构造的安卓框架,包括下面这些基本功能 DI依赖注入,可以注入V ...
- SwiftUI 官方教程(一)
完整中文教程及代码请查看 https://github.com/WillieWangWei/SwiftUI-Tutorials 创建和组合 View 此部分将指引你构建一个发现和分享您喜爱地方的 ...
- Sentry 后端监控 - 最佳实践(官方教程)
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
随机推荐
- Android Touch事件传递机制具体解释 上
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/37961997 近期总是遇到关于Android Touch事件的问题,如:滑动冲突的问题,曾经 ...
- iOS 开发http post 文件的上传
iOS开发网络篇—文件的上传 说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中.本文介绍如何不借助第三方框架实现iOS开发中得文件上传. 由于过程较为复杂,因此本文只贴出部分关键代 ...
- javascript学习(10)——[知识储备]链式调用
上次我们简单的说了下单例的用法,这个也是在我们java中比较常见的设计模式. 今天简单说下链式调用,可能有很多人并没有听过链式调用,但是其实只要我简单的说下的话,你肯定基本上都在用,大家熟知的jQue ...
- PHP使用纯真IP数据库
#纯真数据库调用函数(需要下载纯真数据库文件) function convertip($ip) { $ip1num = 0; $ip2num = 0; $ipAddr1 =""; ...
- ZOJ 1893 A Multiplication Game 【简单博弈】
感觉ZJU上有不少博弈的题目. 这道题目还是比较好理解的,题目大概意思是:两人轮流乘一个2-9的数,从1开始乘,求谁的乘积先大于N. 还是寻找必败点必胜点,不过在这个题目里转换成了寻找必败区间必胜区间 ...
- POJ 2451 Uyuw's Concert(半平面交nlgn)
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> # ...
- HttpResponseRedirect VS HttpResponse
当我们处理了post提交的数据之后,我们使用HttpResponseRedirect跳转到另一个页面,而不是用HttpResponse. 例如当一个投票环节时使用HttpResponse可以使用浏览器 ...
- java计算两个日期相差多少天
java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...
- vnc server配置、启动、重启与连接,图形管理linux系统
环境:RedHat Linux 5企业版.Xwindows:gnome (红帽默认安装的图形界面) 尽管我们可以使用SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的, ...
- [置顶] 图书推荐:SQL Server 2012 T-SQL基础 Itzik Ben-Gan
经过近三个月的不懈努力,终于翻译完毕了.图书虽然是基础知识,但是,即使你已经使用T-SQL几年,很多地方还是能够弥补你的知识空白.大师级的人物写基础知识,或许你想知道这基础中还有哪些深奥,敬请期待吧. ...