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

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. FZOJ--2221-- RunningMan 福建第六届省赛

    题目链接:http://acm.hust.edu.cn/vjudge/contest/127149#problem/J 题目大意: 因为总共就分三个队,因为两个队都要选取最优的策略,不论B队咋放,要使 ...

  2. JavaScript高级程序设计22.pdf

    操作节点 appendChild()用于向childNodes列表末尾添加一个节点,appendChild()返回新增的节点 var returnedNode=someNode.appendChild ...

  3. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  4. [Unix.C]Files and Directories

    stat, fstat, and lstat Functions  本部分讨论的内容主要围绕3个stat函数及其返回值. #include <sys/stat.h> int stat(co ...

  5. Get familiar with key Frameworks of ios

    Frameworks make your life easier as an iOS Developer. They allow you to reuse code written by other ...

  6. windows下redis的安装配置和php扩展使用phpredis

    1. 首先安装先下载redis数据库     下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload 目前是2.02 ...

  7. 用slf4j统一管理日志总结

    用slf4j统一管理日志总结 参考网页:http://www.slf4j.org/ 一.使用slf4j统一管理并配置统一使用log4j日志 使用的jar:(slf4j-api-1.7.5.jar,jc ...

  8. CSS Sprites的详细使用步骤

    一.把小图放在一张大图中,先排版好.上几张图看看,就比如这个: 谷歌: 淘宝: 土豆右下角悬浮框: 1.把用到的小图都放到了一张大图里,其中的小图之间的排版是有点规律的,比如说淘宝那张,类似的小图放置 ...

  9. [D3] 9. Scatter Plot

    Up until now we've just looked at bar charts. A handy chart, no doubt, but D3 offers a variety of ch ...

  10. 将PL/SQL代码封装在机灵的包中

    将代码封装在机灵的包中 http://www.oracle.com/technetwork/issue-archive/2013/13-jan/o13plsql-1872456.html 绝大多数基于 ...