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

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. 根据地址查询经纬度.html

    <html xmlns="http://www.w3.org/1999/xhtml"><head> <title>根据地址查询经纬度</t ...

  2. html背景为灰色 不能操作,中间div可以操作

    <container style="position: absolute; top: 0px; left: 0px; width: 0px; height: 0px; z-index: ...

  3. Java中static和final的区别

    static是静态修饰关键字,可以修饰变量和程序块以及类方法: 当定义一个static的变量的时候jvm会将将其分配在内存堆上,所有程序对它的引用都会指向这一个地址而不会重新分配内存: 当修饰一个程序 ...

  4. 一步一步写一个简单通用的makefile(二)

    这一篇源代码沿用上一篇的源代码hellomake.c hellofunc.c hellofunc.h makefile 但是代码内容和结构有变化,如下: . ├── include │   └── h ...

  5. 51单片机连接24C02-C语言测试代码

    忙了一天多终于透彻了,自己写的不好使,用别人的逐步分析改成自己的,我写得非常简洁易懂. 我总结3点需要注意的地方 1.关闭非IIC通信器件,比如我的开发板SDA和SCL也连接了DS1302,造成干扰会 ...

  6. HDU 1568 Fibonacci 数学= = 开篇

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1568 分析:一道数学题 找出斐波那契数列的通项公式,再利用对数的性质就可得到前几位的数 斐波那契通项公 ...

  7. Tornado自定义分布式session框架

    一.session框架处理请求执行的流程: 1.服务器端生成随机的cookie字符串 2.浏览器发送请求,服务器将cookie返回给浏览器. 3.服务器在生成一个字典.字典的key为cookie,va ...

  8. hdoj 1564 Play a game

    Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. php开发中的页面跳转方法总结

    PHP页面跳转实现的功能就是将网站中一个网页跳转到另一个网页中.对于刚刚学习PHP语言的朋友来说,是必须要掌握的基础方法. 页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自 ...

  10. Http 缓存

    常用Http缓存页asp.net 页面上加上 <%@ OutputCache Duration="10" VaryByParam="None" %> ...