首页 > 代码库 > boost::xml————又一次失败的尝试
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 DealXml16 {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 try118 {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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。