尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码)

请看源码:

DealXml.h

  1. #pragma once
  2.  
  3. #include <string>
  4.  
  5. #include <boost/property_tree/ptree.hpp>
  6.  
  7. struct TestData
  8. {
  9. int var_int;
  10. std::string var_string;
  11. std::wstring var_wstring;
  12.  
  13. };
  14.  
  15. class DealXml
  16. {
  17. public:
  18. typedef boost::property_tree::ptree ptree_type;
  19. typedef boost::property_tree::wptree wptree_type;
  20. DealXml(void);
  21. ~DealXml(void);
  22.  
  23. bool read_xmlW(std::basic_istream<wptree_type::key_type::value_type>& bis);
  24. bool write_xmlW(std::basic_ostream<wptree_type::key_type::value_type>& bos);
  25. bool open_file_and_read_xmlW(const std::string &filepath);
  26. bool open_file_and_write_xmlW(const std::string &filepath);
  27. private:
  28. ptree_type pt;
  29. wptree_type wpt;
  30. TestData m_TestData;
  31. };

DealXml.cpp

  1. #include "DealXml.h"
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. #include <boost/property_tree/xml_parser.hpp>
  8. #include <boost/property_tree/detail/xml_parser_flags.hpp>
  9. #include <boost/foreach.hpp>
  10.  
  11. using namespace std;
  12. using namespace boost;
  13.  
  14. DealXml::DealXml(void)
  15. {
  16. }
  17.  
  18. DealXml::~DealXml(void)
  19. {
  20. }
  21.  
  22. bool DealXml::read_xmlW(std::basic_istream<wptree_type::key_type::value_type>& bis)
  23. {
  24. bool is_success = false;
  25.  
  26. do
  27. {
  28. try
  29. {
  30. boost::property_tree::xml_parser::read_xml(bis, wpt, boost::property_tree::xml_parser::no_concat_text|boost::property_tree::xml_parser::trim_whitespace);
  31.  
  32. std::wstring wstr_test = wpt.get<std::wstring>(L"root.<xmlattr>.value");
  33.  
  34. BOOST_FOREACH( wptree_type::value_type &v, wpt.get_child(L"root") )
  35. {
  36. if ( L"ceng1"==v.first )
  37. {
  38. //无法获取xml数据
  39. m_TestData.var_wstring = v.second.get_value<std::wstring>(L"ceng1");
  40. m_TestData.var_wstring = v.second.get_value<std::wstring>();
  41. //wchar_t *p = v.second.get_value<wchar_t*>(L"ceng1");
  42. //m_TestData.var_wstring = v.second.get<std::wstring>(L"ceng1");
  43. //m_TestData.var_int = v.second.get_value<int>(L"ceng1");
  44. //m_TestData.var_string = v.second.get_value<std::string>(L"ceng1");
  45. //std::cout << v.second.get_value<std::wstring>(L"ceng1") << std::endl;
  46. std::wcout << v.second.get_value<std::wstring>(L"ceng1") << std::endl;
  47. is_success = true;
  48. }
  49. }
  50.  
  51. }
  52. catch(const std::exception &e)
  53. {
  54. std::cout << e.what() << std::endl;
  55. }
  56.  
  57. } while (false);
  58.  
  59. return is_success;
  60. }
  61. bool DealXml::write_xmlW(std::basic_ostream<wptree_type::key_type::value_type>& bos)
  62. {
  63. bool is_success = false;
  64.  
  65. do
  66. {
  67. try
  68. {
  69. boost::property_tree::xml_parser::xml_writer_settings<wchar_t> settings(L'\t', , L"utf-8");
  70. boost::property_tree::xml_parser::write_xml<wptree_type>(bos, wpt, settings);
  71. }
  72. catch(const std::exception &e)
  73. {
  74. std::cout << e.what() << std::endl;
  75. }
  76.  
  77. } while (false);
  78.  
  79. return is_success;
  80. }
  81. bool DealXml::open_file_and_read_xmlW(const std::string &filepath)
  82. {
  83. bool is_success = false;
  84.  
  85. do
  86. {
  87. try
  88. {
  89. std::basic_filebuf<wchar_t> bfb;
  90. if (bfb.open(filepath.c_str(), std::ios::in))
  91. {
  92. std::basic_istream<wchar_t> bis(&bfb);
  93. read_xmlW(bis);
  94. bfb.close();
  95. is_success = true;
  96. }
  97. }
  98. catch(const std::exception &e)
  99. {
  100. std::cout << e.what() << std::endl;
  101. }
  102.  
  103. } while (false);
  104.  
  105. return is_success;
  106. }
  107. bool DealXml::open_file_and_write_xmlW(const std::string &filepath)
  108. {
  109. bool is_success = false;
  110.  
  111. do
  112. {
  113. try
  114. {
  115. std::basic_filebuf<wchar_t> bfb;
  116. if (bfb.open(filepath.c_str(), std::ios::out))
  117. {
  118. std::basic_ostream<wchar_t> bos(&bfb);
  119. write_xmlW(bos);
  120. bfb.close();
  121. is_success = true;
  122. }
  123. }
  124. catch(const std::exception &e)
  125. {
  126. std::cout << e.what() << std::endl;
  127. }
  128.  
  129. } while (false);
  130.  
  131. return is_success;
  132. }

boost::xml————又一次失败的尝试的更多相关文章

  1. 一次失败的尝试hdfs的java客户端编写(在linux下使用eclipse)

    一次失败的尝试hdfs的java客户端编写(在linux下使用eclipse) 给centOS安装图形界面 GNOME桌面环境 https://blog.csdn.net/wh211212/artic ...

  2. boost::xml——基本操作以及中文乱码解决方案 (续)

    本博文主要想说明以下两点: 1.对于上一篇的<boost::xml——基本操作以及中文乱码解决方案>解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题 ...

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

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

  4. 在java下面配置xml导致idea编译失败

    1.今天遇到一个问题在java配置dao.xml的时候导致,idea编译失败 然后把pom.xml上面的 <build> <resources> <resource> ...

  5. springcloud-1: 用官方的pom.xml配置添加依赖失败

    在eclipse中用STS生成了一个springcloud应用,pom.xml的核心配置如下: <parent> <groupId>org.springframework.bo ...

  6. LeetCode 失败的尝试 10. regular expression matching & 正则

    Regular Expression Matching 看到正则就感觉头大,因为正则用好了就很强大.有挑战的才有意思. 其实没有一点思路.循环的话,不能一一对比,匹配模式解释的是之前的字符.那就先遍历 ...

  7. 失败的尝试,使用继承扩展数组,以及ES6的必要性

    我们都知道直接在原生对象上扩展对象是很不好的.所以prototype这样的库广受非议. 一些库,比如lodash采用了工具包形式的扩展方式,绕开了对象的继承. 由于es6的class的出现,我尝试以A ...

  8. ubuntu 18.04下greenplum安装笔记(二)安装Greenplum的失败的尝试

    之前对Linux环境进行了搭建,现在开始进行Greenplum的正式安装. 下载 进Greenplum的官网:https://greenplum.org/download/ 可以发现,对于ubuntu ...

  9. osgEarth开发之OSG解构——失败的尝试

    概述 本文在吸收了<最长的一帧>以及相关参考资料的基础之上解读OSG的基础数据结构,渲染方法. 实现 在这第一部分里,要理解的是run函数的实现,因为以下这一段证明了它的重要性和强大的能力 ...

随机推荐

  1. 今天遇到的mouseout和mouseleave之坑

    时间紧  简短的说就是: mouseout是离开其子元素也会触发 mouseleave是离开指定的那个元素才触发

  2. HTML页面规范

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Oracle 视图添加主键

    在Entity Framework中,从数据库生成模型,视图常报无主键. 解决办法:为试图添加主键/复合主键 create or replace view view_activebudgetamoun ...

  4. node begining

    node begining */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola," ...

  5. 开发小技巧:C#逐个输出字符

    静态自定义方法: static int counter = 0; static string displayString = "This string will appear one let ...

  6. Raspberry Pi Kernel Compilation 内核编译官方文档

    elinux.org/Raspberry_Pi_Kernel_Compilation#Use_the_provided_compiler Software & Distributions: S ...

  7. 数字集成电路设计-8-一个简单sobel图像边缘检测加速器的设计,实现,仿真与综合

    引言 图像视频处理等多媒体领域是FPGA应用的最主要的方面之一,边缘检测是图像处理和计算机视觉中的基本问题,所以也是最常用的,随着数据量的不断增加以及对实时性的要求,一般软件已经不能满足实际需要,这时 ...

  8. WinForm TextBox自定义扩展方法数据验证

    本文转载:http://www.cnblogs.com/gis-crazy/archive/2013/03/17/2964132.html 查看公司项目代码时,存在这样一个问题:winform界面上有 ...

  9. oracle强化练习之分组函数

    1.      显示平均工资为>2000的职位 select job,avg(sal) from emp group by job having avg(sal)>2500; 2.     ...

  10. [ES7] Exploring ES2016 Decorators

    Original artial --> link How descorator looks like: @mydecorator function myFun(){ ... } Descorat ...