boost::xml——基本操作以及中文乱码解决方案 (续)
本博文主要想说明以下两点:
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——基本操作以及中文乱码解决方案 (续)的更多相关文章
- boost::xml——基本操作以及中文乱码解决方案
下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...
- JSP中pageEncoding和charset区别,中文乱码解决方案(转载)
转载自:JSP中pageEncoding和charset区别,中文乱码解决方案 JSP指令标签中<%@ page contentType="text/html;charset=GB23 ...
- Java中文乱码解决方案
Java中文乱码解决方案 1.中文乱码解决方案,确保每个文件的默认编码是UTF-8 加入 URIEncoding="UTF-8" 代码中的设置 1>在se ...
- (转)JSP HTML JAVASCRIPT 中文乱码 解决方案 大全
JSP HTML JAVASCRIPT 中文乱码 解决方案 大全 JSP的中文字符一直是各位初学者首先要解决的问题,下面进行了总结,也给出了解决办法.C4.1 HTML中文编码转换 在JSP文件中的静 ...
- AJAX中文乱码解决方案
通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...
- aspx页面,中文乱码解决方案
由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...
- 基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案
基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案 两个月前做C++课设的时候,电脑编译器编译结果出现了中文乱码,寻求了百度和大神们,都没有解决这个问题,百度上一堆解释是对编译器 ...
- JS传值中文乱码解决方案
JS传值中文乱码解决方案 一.相关知识 1,Java相关类: (1)java.net.URLDecoder类 HTML格式解码的实用工具类,有一个静态方法:public static String ...
- Eclipse中文乱码解决方案
Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...
随机推荐
- HDOJ/HDU 1982 Kaitou Kid - The Phantom Thief (1)(字符串处理)
Problem Description Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and c ...
- [置顶] shell变量赋值-linux
Shell变量赋值 命名须规则: 1)使用变量无需事先声明 2)首个字符必须为字母(a-z,A-Z) 3)中间不能有空格,可以使用下划线(_) 4)不能使用标点符号 5)不能使用bash里的关键字(可 ...
- 命令 "sudo -H" 中的这个 "H" 什么作用?
脚本中使用$HOME变量 问题描述:某些同事原来写的脚本中包含如下内容. BIN_DIR=${HOME}/tools TAIR_BIN_DIR=${HOME}/tair_bin TAIR_SRC_DI ...
- Spring实例化bean的三种方法
1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...
- ASP.NET MVC- Controllers and Routing- Controller Overview
In this tutorial, Stephen Walther introduces you to ASP.NET MVC controllers. You learn how to create ...
- ios 实现跳转到评价界面的两种方式
要想在App内跳转到特定App的详情页或者评论页,首先需要获取到App的id.在 iTunes Connect网站上登陆之后,选择“我的App”,然后点击某个特定的App进入,在App信息的综合信息中 ...
- jquery validate.js表单验证的基本用法入门
这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录. 先贴一个国内某大公司的代码: 复制代码 代码如下: <script type="text/javascript&quo ...
- Android开发:如何安全的中止一个自定义线程Thread
http://blog.csdn.net/yanzi1225627/article/details/8582078 经研究,我推荐这种写法: /*自定义线程*/ class MyThread impl ...
- SVN工具的使用 和在Eclipse中安装GPD插件:(多步审批流,因此选择使用工作流(JBPM)来实现)
前言 重点解说SVN工具的还原版本号. 1.提交svn之前.要先更新文件.假设更新之后有版本号冲突的话.就线下解决掉冲突,在把该文件标记为已经解决冲突. 正文 使用SVN还原历史版本号 water ...
- hadoop错误java.io.IOException Failed to replace a bad datanode on the existing pipeline due to no more good datanodes being available to try
错误: java.io.IOException: Failed to replace a bad datanode on the existing pipeline due to no more go ...