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

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. HW4.26

    public class Solution { public static void main(String[] args) { double sum; double item; for(int i ...

  2. Java的垃圾回收概述

    Java语言建立了垃圾收集机制,即GC,用以跟踪正在使用的对象和发现并回收不再使用的对象,垃圾清理势在必行,以下讲述java垃圾收集算法. 1.Java垃圾收集算法的核心思想 Java语言建立了垃圾收 ...

  3. .Net设计模式_适配器模式

    引言: 有一次我看到一个朋友拿出一个三角型的东西,好奇的上去一看你,我勒个去,传说中的万能插口转接器(插口适配器). 一面是插入口,集合了我认识的所有插口的形状,也有国内没看见过的:另一个是跟插座接触 ...

  4. request&response笔记

    一.HttpServletResponse 1.代表了一个响应 2.应用: 2.1输出中文 字节流输出中文 //拿到输出字节流对象 ServletOutputStream oos = response ...

  5. Linux的文件属性

    在Linux中,文件的拥有者可以将文件的属性设置成三种属性,可读(r).可写(w)和可执行(x).文件又分为三个不同的用户级别,文件的拥有者(u),文件的所属组(g),其他用户(o). 第一个字符显示 ...

  6. saveFileDialog

    saveFileDialog1.ShowDialog saveFileDialog.FileName 设置的时候是一个字符串. 如: 新建 RTF 文档.rtf 获得的时候 则为一个完整的路径. 如: ...

  7. [Javascript] MetaProgramming: function name

    Each function should have a 'name' property. It can be anonymous, empty, the same as function name, ...

  8. C++数据结构和算法每天一练(线性表)

    #include <iostream> using namespace std;  class  ArrayLinerTable { public:       void InitLine ...

  9. dll注册到GAC还是bin - sharepoint程序

    通常来说程序在使用dll的时候,会先去GAC中查找是否有存在合适的dll,然后才会到应用程序下的bin目录去查找: 前几天遇到了一个奇葩问题,web项目工程添加了一个第三方dll的引用,然后把这个第三 ...

  10. "ORA-00942: 表或视图不存在 "的原因和解决方法

    "ORA-00942: 表或视图不存在 "的原因和解决方法   采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或 ...