关于 Boost.PropertyTree字符集
来源:互联网 发布:阿里云文件管理系统 编辑:程序博客网 时间:2024/06/03 18:30
http://notes.xj-labs.net/?p=52
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 操作库 ,有非常好的性能(据说)。
- 关于 Boost.PropertyTree字符集
- 使用Boost.PropertyTree处理XML、JSON和INI数据
- 基于boost.propertytree的XML文件读写类
- 用boost的PropertyTree json_parser 生成json字符串
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于字符集
- 关于Boost
- 关于字符集的测试报告
- 关于MySql字符集问题
- 关于字符集和编码
- 关于Unicode字符集编程
- Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标
- TCP/UDP相关知识总汇
- CREATE_RECORD或者NEXT_RECORD导致 FRM-40102 错误 .
- Android布局之LinearLayout与RelativeLayout的属性
- 直方图均衡化
- 关于 Boost.PropertyTree字符集
- 巧用word2007插件功能实现PDF转换
- ubuntu 10.10 编译Android2.2问题解决
- 模拟开关CD4051使用的4个要点
- 非常重要的作用在起源的市场
- poj 1742 coins 背包问题
- AJAX应用的五个步骤
- 发展一个更战略方法处理这种业务
- qt按钮贴图