使用Boost.PropertyTree处理XML、JSON和INI数据
Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。
- 主要作用/应用场合
Boost.PropertyTree 提供了一种结构化的数据存储容器。并且提供了一系列的解释器可以将内存中的结构与具体格式相互转换 (比如: INI, XML, JSON )。
至少可以用在:
- 进程间通讯或者跨语言的进程间的通讯
- 一些配置文件的存取
- 网络通讯协议的格式
- 基本用法
基本用法有 2 种场景。第一种是从 Property Tree存储到具体格式。第二种是从具体格式解析到具体的 Property Tree。其他还有一些 Property Tree 操作的方法,比如:遍历、搜索等方法。
以下这个 Sample 就是基本用法的测试:
先把 数据存储到 datum 中,随后输出 相应的 XML 和 JSON 到 std::cout 上。最后再从 JSON Stream 中解析输入到 ptParse 中获得相应 的数据。
#include <stdio.h> #include <iostream>
#include <sstream>
#include <string>
#include <locale> #include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp" int main(int argc, char **argv)
{
/* The data format
* <root>
* <num>1</num>
* <str>Test</str>
* </root>
*/
try
{
/* create the property tree */
boost::property_tree::ptree datum;
datum.put("root.num", 100);
datum.put("root.str", "string"); /* output XML string */
std::ostringstream xmlOutputStream;
boost::property_tree::xml_parser::write_xml(xmlOutputStream,
datum);
std::cout << "XML format:" << std::endl;
std::cout << xmlOutputStream.str() << std::endl; /* output JSON string */
std::ostringstream jsonOutputStream;
boost::property_tree::json_parser::write_json(jsonOutputStream,
datum);
std::cout << "JSON format:" << std::endl;
std::cout << jsonOutputStream.str() << std::endl; /* read datum from JSON stream */
boost::property_tree::ptree ptParse;
std::istringstream jsonIStream;
jsonIStream.str(jsonOutputStream.str());
boost::property_tree::json_parser::read_json(jsonIStream,
ptParse);
int num = ptParse.get<int>("root.num");
std::string strVal = ptParse.get<std::string>("root.str");
std::cout << "Num=" << std::dec << num
<< " Str=" << strVal << std::endl << std::endl;
}
catch (...)
{
printf("create boost::property_tree::ptree failed\n");
} return 0;
}
- 关于字符集
Boost 目前是支持 UTF8 的,但是不能用 直接用 Unicode。所以,如果要存储宽字符就有点麻烦需要用到 Boost 提供的 utf8_codecvt_facet 做转换。
下面就是一个存储 wchar_t 的 Sample:
和之前的其实差不多,有 2 点主要不同。一是用了 wptree 替换了 ptree。二是增加了 utf8_codecvt_facet 在相应的 Stream 里做转换。
#include <stdio.h> #include <iostream>
#include <sstream>
#include <string>
#include <locale> #include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp" #include "boost/program_options/detail/convert.hpp"
#include "boost/program_options/detail/utf8_codecvt_facet.hpp" int main(int argc, char **argv)
{
/* The data format
* <root>
* <num>1</num>
* <str>Test</str>
* </root>
*/
/* test UTF-8 format */
try
{
/* create boost utf8 codecvt */
std::locale oldLocale;
std::locale utf8Locale(oldLocale,
new boost::program_options::detail::utf8_codecvt_facet());
std::wcout.imbue(utf8Locale); /* create the wptree for save the UTF-8 data */
boost::property_tree::wptree datum;
datum.put(L"root.num", 100);
datum.put(L"root.str", L"wstring"); /* output XML string */
std::wostringstream xmlOutputStream;
xmlOutputStream.imbue(utf8Locale);
boost::property_tree::xml_parser::write_xml(xmlOutputStream,
datum);
std::wcout << L"XML format:" << std::endl;
std::wcout << xmlOutputStream.str() << std::endl; /* output JSON string */
std::wostringstream jsonOutputStream;
jsonOutputStream.imbue(utf8Locale);
boost::property_tree::json_parser::write_json(jsonOutputStream,
datum);
std::wcout << L"JSON format:" << std::endl;
std::wcout << jsonOutputStream.str() << std::endl; /* read datum from JSON stream */
boost::property_tree::wptree wptParse;
std::wistringstream jsonIStream;
jsonIStream.imbue(utf8Locale);
jsonIStream.str(jsonOutputStream.str());
boost::property_tree::json_parser::read_json(jsonIStream,
wptParse);
int num = wptParse.get<int>(L"root.num");
std::wstring wstrVal = wptParse.get<std::wstring>(L"root.str");
std::wcout << L"Num=" << std::dec << num
<< L" Str=" << wstrVal << std::endl << std::endl;
}
catch (...)
{
printf("create boost::property_tree::wptree failed\n");
} return 0;
}
- 附录
- 以上的测试程序,在 Boost 1.42.0 和 MS VS 2008 上测试过。这里是打包文件 PTreeTest
- 在 Boot.org 上能找到更多的 PropertyTree 的操作。PropertyTree 在 Boost 1.41.0 版本的手册。最好去看新版本的如果以后更新的话。
- Boot.PropertyTree 用的 XML 解析器是 RapidXML。是一个基于模板设计的 XML 操作库 ,有非常好的性能(据说)。
原文地址:http://notes.xj-labs.net/?p=52
使用Boost.PropertyTree处理XML、JSON和INI数据的更多相关文章
- Boost.PropertyTree读取ini文件(Linux环境)
昨天因为需要读取配置文件略略伤神.网上很多例子但是我用都会报错,很多人把Boost.PropertyTree的函数写很麻烦的包所以报错我也不知道什么问题,所以今天整理下. 头上附上官网链接:Boost ...
- boost propertyTree
Boost PropertyTree provides a tree structure to store key/value pairs. Tree structures means that a ...
- 模块简介:(random)(xml,json,pickle,shelve)(time,datetime)(os,sys)(shutil)(pyYamal,configparser)(hashlib)
Random模块: #!/usr/bin/env python #_*_encoding: utf-8_*_ import random print (random.random()) #0.6445 ...
- Xml,Json,Hessian,Protocol Buffers序列化对比
简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...
- xml json protobuf
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...
- iOS开发笔记3:XML/JSON数据解析
这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...
- Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题
1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...
- [WEB API] CLIENT 指定请求及回应格式(XML/JSON)
[Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...
- string xml json格式区别
string 是一种最普通的储存一串字符的数据格式 xml 是一种可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 它非常适合万维网传输,提供统一的方 ...
随机推荐
- iOS中谓词的使用
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...
- [置顶] 吃论扯谈---吃货和Office 365订阅的关系
什么事物都可以和吃联系起来,在女孩子穿裙子的季节这是一个悲伤的故事! 说明: :Office365是微软云计算产品之一,其采取订阅的方式,按人头*每月的方式付费,用户可以选择自己需要的服务 2:Off ...
- LNK 2005 error 函数定义也是定义!!
url=Ccne-rWwUO9tJp5YAPcycUw09__2whgZLpLw2aWVuYuE-fhu46kaVNX4BldWlsxig1tDML47aO_ctD3PcUlGjK"> ...
- 随手记今天跟的几个iOS项目代码的问题
休了一阵子假期,今天刚回来上班,项目代码已经有挺大的变化了,我就先体验.发现.跟进问题. 第一个问题是点击某个cell就挂掉的现象,同事表示必现但挺神奇.由于挂掉的时候没有啥有意义的信息,所以先简单粗 ...
- CentOS6.5实现rsync+inotify实时同步
参考博文: 参考1:CentOS6.5实现rsync+inotify实时同步 参考2:inotify-tools+rsync实时同步文件安装和配置 CentOS 6.3下rsync服务器的安装与配置 ...
- 使用zabbix监控nginx
在zabbix agentd客户端上,查看nginx是否加载了--with-http_stub_status_module.因为zabbix监控nginx是根据 nginx的Stub Status模块 ...
- BFS 、DFS 解决迷宫入门问题
问题 B: 逃离迷宫二 时间限制: 1 Sec 内存限制: 128 MB提交: 12 解决: 5[提交][状态][讨论版] 题目描述 王子深爱着公主.但是一天,公主被妖怪抓走了,并且被关到了迷宫. ...
- Impossible WPF Part 1: Binding Properties
原文 http://www.11011.net/wpf-binding-properties Ever wanted to write the following? <RichTextBoxDo ...
- docker学习笔记:容器的网络设置
创建一个docker容器,docker系统会自动为该容器分配一个ip地址,通常是172.17开头. 我们可以在主机上用 docker inspect 命令 或者进入容器用ifconfig命令来查看容器 ...
- Vedis - An Embeddable Datastore Engine
Vedis - An Embeddable Datastore Engine An Embeddable Datastore Engine Tweet Follo ...