boost::xml————又一次失败的尝试

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

boost::xml————又一次失败的尝试

时间: 2024-08-01 16:17:50

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

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

下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现普通字符串和宽字符串的傻瓜切换(模仿tchar.h)4.代码运行环境为VS2010,需要导入boost库才能正常运行5.VS2010运行时可能会发生错误.例如:cl.exe 或者 cvtres.exe 报错. 解决办法就是重新打开项目或者切换其它正常项目运行一下(反正我是这么解决的) 下面是源码部分

Mac自己搭建爬虫搜索引擎(nutch+elasticsearch是失败的尝试,改用scrapy+elasticsearch)

1.引言 项目需要做爬虫并能提供个性化信息检索及推送,发现各种爬虫框架.其中比较吸引的是这个: Nutch+MongoDB+ElasticSearch+Kibana 搭建搜索引擎 E文原文在:http://www.aossama.com/search-engine-with-apache-nutch-mongodb-and-elasticsearch/ 考虑用docker把系统搭建起来测试: docker来源如下: https://www.elastic.co/guide/en/elastics

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

我们都知道直接在原生对象上扩展对象是很不好的.所以prototype这样的库广受非议. 一些库,比如lodash采用了工具包形式的扩展方式,绕开了对象的继承. 由于es6的class的出现,我尝试以Array派生子对象的方式进行扩展. 以下是一个简单的例子. "use strict" class Abc extends Array{ getarray(){ return this; } } let x=new Abc(5).fill(1); console.log(x.getarray

不吐不快和一次失败的尝试

一定要在工作日每天下班前,作总结,切记,切记. 上午,处理周六遗留下来的问题,听朱X贵爆料高层腐败,事儿觉得未来一片灰暗. 不论真假,这些恶,自己是万万不能做的,如果是为了钱和名,不要也罢. 昨晚看洛克菲勒自传,更加激励自己全身心投入到工作中去. 人生在世,草木一春,钱,名,女人带不进坟墓.望古今,能有发明.作品.思想传世,足矣. 杂活忙完,看了看linux的命令,尝试了一些新的命令,可能是我们设备的linux是经过了特殊的整理,导致一些命令失效. 但一时兴起,觉得应该设计一套文件命令和打包命令

失败的尝试 10. regular expression matching &amp; 正则

Regular Expression Matching 看到正则就感觉头大,因为正则用好了就很强大.有挑战的才有意思. 其实没有一点思路.循环的话,不能一一对比,匹配模式解释的是之前的字符.那就先遍历模式把. ... 中间 n 次失败的提交 感觉代码逻辑很乱.重新捋一下再动手写. 找几个重点分析一下: Wrong Answer: Input: "aaa" "ab*a*c*a" Output: false Expected: true 调试 aaa ab*a*c*a

配置server.xml后,启动tomcat 失败(Unable to start cluster)及解决方法

在配置负载均衡环境过程中修改server.xml  后重启tomcat报错,报错信息如下 [[email protected] bin]# ./catalina.sh runUsing CATALINA_BASE: /usr/local/TC6_AUsing CATALINA_HOME: /usr/local/TC6_AUsing CATALINA_TMPDIR: /usr/local/TC6_A/tempUsing JRE_HOME: /usr/java/jdk1.6.0_45Using CL

&lt;Spring实战&gt;3:最小化Spring XML配置

1 自动装配 Bean 属性 1.1 4 种类型的自动装配 byName:把与 Bean 的属性具有相同名字或 ID 的其他 Bean 自动装配到 Bean 的对应属性中 byType:把与 Bean 的属性具有相同类型的其他 Bean 自动装配到 Bean 的对应属性中 constructor:把与 Bean 的构造器入参具有相同类型的其他 Bean 自动装配到 Bean 构造器的对应入参中 autodetect:首先尝试使用 constructor 进行自动装配,如果失败再尝试使用 byTy

ajax(Asynchronous JavaScript + XML) 技术学习

参考文档:https://developer.mozilla.org/en-US/docs/AJAX 本文进行了大致翻译. Ajax 本身本不是一门技术,而是在2005年由Jesse James Garrett首创的描述为一个"新"途径来应用许多已存在的技术,包括:HTML 或者 XHTML, Cascading Style Sheets, JavaScript, The Document Object Model, XML, XSLT, 和最重要的 XMLHttpRequest ob

Matlab中使用脚本和xml文件自动生成bus模块

帮一个老师写的小工具 在一个大工程中需要很多bus来组织信号,而为了规范接口,需要定义很多BusObject,用Matlab语言手写这些BusObject比较费工夫 所以用xml配置文件来写,也便于更改总线数据接口,然后使用matlab脚本来生成BusObject和Bus模块库 以下代码运行环境:WIN10+Matlab2015a 下面给出代码Matlab函数的代码: function xmlbuscreator(xmlfile) % XMLBUSCREATOR:从xml文件读取数据结构,并生成