本博文主要想说明以下两点:

1.对于上一篇的《boost::xml——基本操作以及中文乱码解决方案》解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题。

可以参看这段代码

 bool read_xmlW(const std::string &xml,boost::property_tree::wptree &a_wptree)
{
bool rt = true;
boost::mutex::scoped_lock s_lock(m_mutex);
std::wstring wstr;
wsstream.clear(); wstr = g_codetrans()->utf8_to_utf16(xml);
wsstream << wstr; // set facet for std::wstring
std::locale current_locale(std::locale(""), new boost::program_options::detail::utf8_codecvt_facet()); try
{
//boost::property_tree::read_xml<boost::property_tree::wptree>(wsstream, a_wptree, boost::property_tree::xml_parser::trim_whitespace, current_locale);
boost::property_tree::read_xml<boost::property_tree::wptree>(wsstream, a_wptree, boost::property_tree::xml_parser::trim_whitespace);
}
catch (const std::exception &e)
{
#ifdef _DEBUG
std::cout << "Error:" << typeid(e).name() << ": ";
std::cout << e.what() << std::endl;
#endif // DEBUG
rt = false;
}
return rt; }

注:里面宽字符的a_wptree没有问题,主要是wstr = g_codetrans()->utf8_to_utf16(xml);这个问题。

此段代码仅供参考。

2.对于xml写的一点小技巧,这个问题难了我好久,写在下面以供分享。

先看输出的xml文件

 <?xml version="1.0" encoding="utf-8"?>
<root value="root_test">
<ceng1 value="ceng1_test">
<ceng2 value="0_ceng2"/>
<ceng2 value="1_ceng2"/>
<ceng2 value="2_ceng2"/>
</ceng1>
<ceng1 value="ceng1_test">
<ceng2 value="0_ceng2"/>
<ceng2 value="1_ceng2"/>
<ceng2 value="2_ceng2"/>
</ceng1>
<other1 value="other1_test">
<other2 value="0_other2"/>
<other2 value="1_other2"/>
<other2 value="2_other2"/>
</other1>
</root>

大家不要小看这几个小小层次,难了我好久,读取容易,写出一模一样的难。难点在于对于节点层次的把握。2个ceng1节点和1个other1节点并列,他们的定节点都是root,他们还有子节点。

下面附上片段源码:

 void TestPtree(std::string &o_xml)
{
tptree pt;//对应第一层的节点(根节点)
tptree cccpt;//对应第二层的节点
for (int j= ; j< ; j++)
{
tptree cpt;//对应第三层的节点
for (int i= ; i< ; i++)
{
std::stringstream ss;
ss << i;
string str = ss.str();
str.append("_ceng2");
tptree ccpt;//对应第三层的属性值
ccpt.add<std::string>("<xmlattr>.value", str);
cpt.add_child("ceng2", ccpt);
}
cpt.put<std::string>("<xmlattr>.value", "ceng1_test");//增加第一层的属性
cccpt.add_child("ceng1", cpt);
}
for (int j= ; j< ; j++)
{
tptree cpt;
for (int i= ; i< ; i++)
{
std::stringstream ss;
ss << i;
string str = ss.str();
str.append("_other2");
tptree ccpt;
ccpt.add<std::string>("<xmlattr>.value", str);
cpt.add_child("other2", ccpt);
}
cpt.put<std::string>("<xmlattr>.value", "other1_test");
cccpt.add_child("other1", cpt);
}
cccpt.put<std::string>("<xmlattr>.value", "root_test");//增加根节点的属性
pt.add_child("root", cccpt); #ifdef CHINESE_CHARSET
std::locale current_locale(locale(""), new boost::program_options::detail::utf8_codecvt_facet());
boost::property_tree::xml_parser::xml_writer_settings<wchar_t> settings(L'\t', , L"utf-8");
#else
std::locale current_locale;
boost::property_tree::xml_parser::xml_writer_settings<char> settings('\t', , "utf-8");
#endif try
{
boost::property_tree::write_xml(o_xml, pt, current_locale, settings);
}
catch (const std::exception &e)
{
cout << "Error:" << typeid(e).name() << ": ";
cout << e.what() << endl;
}
}

注:这是主要的代码片段,如果想运行编译通过可以参看我的另一篇博文《boost::xml——基本操作以及中文乱码解决方案》完整源码,地址是:点击这里

boost::xml——基本操作以及中文乱码解决方案 (续)的更多相关文章

  1. boost::xml——基本操作以及中文乱码解决方案

    下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...

  2. JSP中pageEncoding和charset区别,中文乱码解决方案(转载)

    转载自:JSP中pageEncoding和charset区别,中文乱码解决方案 JSP指令标签中<%@ page contentType="text/html;charset=GB23 ...

  3. Java中文乱码解决方案

    Java中文乱码解决方案   1.中文乱码解决方案,确保每个文件的默认编码是UTF-8         加入 URIEncoding="UTF-8" 代码中的设置 1>在se ...

  4. (转)JSP HTML JAVASCRIPT 中文乱码 解决方案 大全

    JSP HTML JAVASCRIPT 中文乱码 解决方案 大全 JSP的中文字符一直是各位初学者首先要解决的问题,下面进行了总结,也给出了解决办法.C4.1 HTML中文编码转换 在JSP文件中的静 ...

  5. AJAX中文乱码解决方案

    通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...

  6. aspx页面,中文乱码解决方案

    由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...

  7. 基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案

    基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案 两个月前做C++课设的时候,电脑编译器编译结果出现了中文乱码,寻求了百度和大神们,都没有解决这个问题,百度上一堆解释是对编译器 ...

  8. JS传值中文乱码解决方案

    JS传值中文乱码解决方案 一.相关知识 1,Java相关类: (1)java.net.URLDecoder类 HTML格式解码的实用工具类,有一个静态方法:public static  String ...

  9. Eclipse中文乱码解决方案

    Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...

随机推荐

  1. arcGis引入Dll报无法嵌入互操作类型错误解决方法

    arcGis引入Dll报“无法嵌入互操作类型"ESRI.ARCGIS.Geometry.PointClass".请改用通用接口."   解决方法:设置引用DLL的“嵌入互 ...

  2. [转]Angular, Backbone, or Ember: Which is Best for your Build?

    In order to choose which framework is right for your build, we've asked four important questions of ...

  3. webview改变网页宽度

    - (void)webViewDidFinishLoad:(UIWebView *)webView { //修改服务器页面的meta的值 NSString *meta = [NSString stri ...

  4. Closure Compiler(封闭编辑器), Closure Inspector, Closure Templates, 封闭图书馆(Closure Library) Google- 摘自网络

    谷歌日前宣布,将自己开发者使用的一系列工具对外开放.这些工具曾用来开发谷歌的主要产品,包括Gmail.谷歌文档(Google Docs)和谷歌地图(Google Maps). 第一个工具叫做Closu ...

  5. 缓存算法–LRU

    LRU LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,也就是说,LRU缓存把最近最少使用的数据移除,让给最新读取的数据.而往往最常读取的,也是读取次数最多的,所 ...

  6. 在html使用a标签 直接下载图片 不通过后台实现直接下载

    由于a标签在HTML中链接图片会被识别并打开到网页上 如果想下载这个图片的话 就需要连接到后台读取文件并生成一个头信息下载.不过可以先给a标签加上一个download属性即可直接下载了. <a ...

  7. FlexboxLayout 的一些基本介绍与基本用法

    1什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方案,可以简便.完整.响应式地实现各种页面布局,并且 React Na ...

  8. js模拟下载

    <a id="downFile" href="" style="display: none;"><span id=&quo ...

  9. Android框架之网络开发框架Volley

    1. Volley简单介绍 我们平时在开发Android应用的时候不可避免地都须要用到网络技术.而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进 ...

  10. cocos2d-x 3.0正式版 环境搭建 (解决载入失败,未能载入XXX包)

    cocos2d-x 3.0正式版最终公布了~ 等了好久~ 废话不多说,直接写方法. 1.下载安装vc2012 ,我用的是旗舰版,传送门:点击打开链接 2.下载cocos2d-x 3.0正式版,传送门: ...