当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误。

网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错。而使用ptree来写中文时不会出错。

综合以上信息,尝试使用ptree来写xml,而用wptree来读。以一个demo来说明吧。

1 //包含文件
2 #include <boost/property_tree/ptree.hpp>
3 #include <boost/property_tree/xml_parser.hpp>
4 #include <boost/property_tree/json_parser.hpp>
5 #include <boost/foreach.hpp>
6 #include <string>
7 #include <exception>
8 #include <iostream>

定义结构体:

1 struct debug_simple
2 {
3 int itsNumber;
4 std::string itsName; //这里使用string就可以
5 void load(const std::string& filename); //载入函数
6 void save(const std::string& filename); //保存函数
7 };

保存函数,使用ptree:

 1 void debug_simple::save( const std::string& filename )
2 {
3 using boost::property_tree::ptree;
4 ptree pt;
5
6 pt.put("debug.number",itsNumber);
7 pt.put("debug.name",itsName);
8
9 write_xml(filename,pt);
10 }

载入函数使用的wptree,读取的值为wstring,需转换成string

 1 void debug_simple::load( const std::string& filename )
2 {
3 using boost::property_tree::wptree;
4 wptree wpt;
5 read_xml(filename, wpt);
6
7 itsNumber = wpt.get<int>(L"debug.number");
8 std::wstring wStr = wpt.get<std::wstring>(L"debug.name");
9 itsName = std::string(wStr.begin(),wStr.end()); //wstring转string
10 }

main函数:

 1 int _tmain(int argc, _TCHAR* argv[])
2 {
3
4 try
5 {
6 debug_simple ds,read;
7 ds.itsName = "汉字english";
8 ds.itsNumber = 20;
9
10 ds.save("simple.xml");
11 read.load("simple.xml");
12
13 std::cout<<read.itsNumber<<read.itsName;
14
15 }
16 catch (std::exception &e)
17 {
18 std::cout << "Error: " << e.what() << "\n";
19 }
20 return 0;
21 }

boost.xml_parser中文字符问题的更多相关文章

  1. boost.xml_parser中文字符问题 (转)

    当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...

  2. Java中文字符处理的四大迷题

    虽然计算机对英文字符的支持非常不错,我们也恨不得写的程序只会处理英文的数据,但是昨为中国人,无可避免地要处理一些中文字符.当很简单的一件事情,遇到了中文,一切就不同了!本文就会讲述实际生产环境中遇到的 ...

  3. Dev Cpp 输出中文字符问题

    最近 c++ 上机作业,vc++6.0 挂了没法用,只好用 Dev Cpp 先顶替一下,然而在遇到输出中文字符的时候出现了乱码的情况,但这种情况又非常诡异.于是简单了解了一下写成此博客. [写在前面] ...

  4. 中文字符匹配js正则表达式

    普遍使用的正则是[\u4e00-\u9fa5],但这个范围并不完整.例如:  /[\u4e00-\u9fa5]/.test( '⻏' ) // 测试部首⻏,返回false    根据Unicode 5 ...

  5. UTF-8和GBK等中文字符编码格式介绍及相互转换

    我们有很多时候需要使用中文编码格式,比如gbk.gb2312等,但是因为主要针对中文编码设置,因此并不完全通用,这样一来就有了在各编码间相互转换的需求,比如和UTF8的转换.可是在我使用的过程中,却发 ...

  6. URL传递中文字符,特殊危险字符的解决方案(仅供参考)urldecode、base64_encode

    很多时候,我们需要在url中传递中文字符或是其它的html等特殊字符,似乎总会有各种乱,不同的浏览器对他们的编码又不一样, 对于中文,一般的做法是: 把这些文本字符串传给url之前,先进行urlenc ...

  7. 使用Java判断字符串中的中文字符数量

    Java判断一个字符串str中中文的个数,经过总结,有以下几种方法(全部经过验证),可根据其原理判断在何种情况下使用哪个方法: 1. char[] c = str.toCharArray(); for ...

  8. python中文字符乱码(GB2312,GBK,GB18030相关的问题)

    转自博主 crifan http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ 在玩wordpress的一个博客搬 ...

  9. poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。

    起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...

随机推荐

  1. java读写

    IO流下分为字节流与字符流,每个流又分为输入输出以及读写. 字节流的两个基类为InputStream与OutputStream. 字符流为Reader和Writer

  2. 条码知识之九:EAN-128条码(上)

     EAN-128码,现称GS1-128码,是专用于GS1系统中的条码,可以标注商品的附加信息,在商品信息的标识.产品的跟踪与追溯中有广泛的用途. EAN-128码来自于CODE-128码,在字符集.条 ...

  3. Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式

    原文 Windows Phone 8初学者开发—第13部分:设置LongListSelector中磁贴的样式 第13部分:设置LongListSelector中磁贴的样式 原文地址: http://c ...

  4. 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 出现上述异常 ...

  5. ThinkPHP - 独立分组项目搭建

    配置文件: <?php return array( //独立分组 'APP_GROUP_LIST' => 'Home,Admin', //分组列表 'APP_GROUP_MODE' =&g ...

  6. linux嵌入式: 实现自己的tree命令

    //# cat treecmd.c #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #inclu ...

  7. 用Flask实现视频数据流传输

    Flask 是一个 Python 实现的 Web 开发微框架.这篇文章是一个讲述如何用它实现传送视频数据流的详细教程. 我敢肯定,现在你已经知道我在O’Reilly Media上发布了有关Flask的 ...

  8. 用QT创建WINDOWS服务程序

    恩, qtservice挺好的http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtservi ...

  9. uva 10271 Chopsticks(dp)

    题目连接:10271 - Chopsticks 题目大意:给出m和n, 然后给出n根筷子从小到大给出, 现在要从这n根筷子中选出m + 8组筷子, 每组筷子包括三根, 现在要求所有m + 8组每组筷子 ...

  10. [置顶] 如何判断两个IP大小关系及是否在同一个网段中

    功能点  判断某个IP地址是否合法 判断两个IP地址是否在同一个网段中 判断两个IP地址的大小关系 知识准备 IP协议 子网掩码 Java 正则表达式 基本原理 IP地址范围 0.0.0.0- 255 ...