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

时间:2024-01-02 18:53:51

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

请看源码:

DealXml.h

 #pragma once

 #include <string>

 #include <boost/property_tree/ptree.hpp>

 struct TestData
{
int var_int;
std::string var_string;
std::wstring var_wstring; }; class DealXml
{
public:
typedef boost::property_tree::ptree ptree_type;
typedef boost::property_tree::wptree wptree_type;
DealXml(void);
~DealXml(void); bool read_xmlW(std::basic_istream<wptree_type::key_type::value_type>& bis);
bool write_xmlW(std::basic_ostream<wptree_type::key_type::value_type>& bos);
bool open_file_and_read_xmlW(const std::string &filepath);
bool open_file_and_write_xmlW(const std::string &filepath);
private:
ptree_type pt;
wptree_type wpt;
TestData m_TestData;
};

DealXml.cpp

 #include "DealXml.h"

 #include <iostream>
#include <fstream>
#include <string> #include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/detail/xml_parser_flags.hpp>
#include <boost/foreach.hpp> using namespace std;
using namespace boost; DealXml::DealXml(void)
{
} DealXml::~DealXml(void)
{
} bool DealXml::read_xmlW(std::basic_istream<wptree_type::key_type::value_type>& bis)
{
bool is_success = false; do
{
try
{
boost::property_tree::xml_parser::read_xml(bis, wpt, boost::property_tree::xml_parser::no_concat_text|boost::property_tree::xml_parser::trim_whitespace); std::wstring wstr_test = wpt.get<std::wstring>(L"root.<xmlattr>.value"); BOOST_FOREACH( wptree_type::value_type &v, wpt.get_child(L"root") )
{
if ( L"ceng1"==v.first )
{
//无法获取xml数据
m_TestData.var_wstring = v.second.get_value<std::wstring>(L"ceng1");
m_TestData.var_wstring = v.second.get_value<std::wstring>();
//wchar_t *p = v.second.get_value<wchar_t*>(L"ceng1");
//m_TestData.var_wstring = v.second.get<std::wstring>(L"ceng1");
//m_TestData.var_int = v.second.get_value<int>(L"ceng1");
//m_TestData.var_string = v.second.get_value<std::string>(L"ceng1");
//std::cout << v.second.get_value<std::wstring>(L"ceng1") << std::endl;
std::wcout << v.second.get_value<std::wstring>(L"ceng1") << std::endl;
is_success = true;
}
} }
catch(const std::exception &e)
{
std::cout << e.what() << std::endl;
} } while (false); return is_success;
}
bool DealXml::write_xmlW(std::basic_ostream<wptree_type::key_type::value_type>& bos)
{
bool is_success = false; do
{
try
{
boost::property_tree::xml_parser::xml_writer_settings<wchar_t> settings(L'\t', , L"utf-8");
boost::property_tree::xml_parser::write_xml<wptree_type>(bos, wpt, settings);
}
catch(const std::exception &e)
{
std::cout << e.what() << std::endl;
} } while (false); return is_success;
}
bool DealXml::open_file_and_read_xmlW(const std::string &filepath)
{
bool is_success = false; do
{
try
{
std::basic_filebuf<wchar_t> bfb;
if (bfb.open(filepath.c_str(), std::ios::in))
{
std::basic_istream<wchar_t> bis(&bfb);
read_xmlW(bis);
bfb.close();
is_success = true;
}
}
catch(const std::exception &e)
{
std::cout << e.what() << std::endl;
} } while (false); return is_success;
}
bool DealXml::open_file_and_write_xmlW(const std::string &filepath)
{
bool is_success = false; do
{
try
{
std::basic_filebuf<wchar_t> bfb;
if (bfb.open(filepath.c_str(), std::ios::out))
{
std::basic_ostream<wchar_t> bos(&bfb);
write_xmlW(bos);
bfb.close();
is_success = true;
}
}
catch(const std::exception &e)
{
std::cout << e.what() << std::endl;
} } while (false); return is_success;
}