```cpp
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include <sstream>
#include <iostream>
#include <string>
//转码才用到
#include <Windows.h>
#include <WinNls.h>
#include <wchar.h>
#include <stringapiset.h>
//<?xml version="1.0" encoding="UTF-8"?>
//<aa>
// <bb>
// <cc>1.0</cc>
// <dd>1.0</dd>
// </bb>
//</aa>
std::string Utf8ToGbk(const char* src_str)//中文感觉还是有问题
{
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}
void GBKToUTF8(char* szOut)
{
char* strGBK = szOut;
int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, NULL, 0);
unsigned short* wszUtf8 = new unsigned short[len + 1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char* szUtf8 = new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL, NULL);
//szOut = szUtf8;
memset(szOut, 0, strlen(szUtf8) + 1);
memcpy(szOut, szUtf8, strlen(szUtf8));
delete[] szUtf8;
delete[] wszUtf8;
}
//将string转换成wstring
std::wstring StringToWstring(const std::string& str)
{
std::wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0'; //添加字符串结尾
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return std::move(result);
}
//将wstring转换成string
std::string WstringToString(const std::wstring& wstr)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return std::move(result);
}
int main()
{
try {
boost::property_tree::ptree xmlpt;
//boost解析 gbk
//std::string aaa = "<?xml version=\"1.0\" encoding=\"GBK\"?><Message><MessageHeader><Version>1.0</Version><From></From><System></System></MessageHeader><MessageBody Type = \"Snap\"><ID>哈哈</ID><LaneNo>啊的布局方式是否还款时间 </LaneNo></MessageBody></Message>";
//std::stringstream b;
//b << aaa;
//boost::property_tree::read_xml(b, xmlpt);
//std::string a = xmlpt.get<std::string>("Message.MessageBody.ID", "100");
//std::string ab = xmlpt.get<std::string>("Message.MessageBody.LaneNo", "100");
//std::cout << a.c_str();
//std::cout << ab.c_str();
//boost解析 gbk
boost::property_tree::read_xml("C:\\Data\\1.txt", xmlpt);//utf-8文件
std::string a = xmlpt.get<std::string>("aa.bb.cc","100");
std::string ab = xmlpt.get<std::string>("aa.bb.dd", "100");
std::cout << Utf8ToGbk(a.c_str());
std::cout << Utf8ToGbk(ab.c_str());
}
catch (...)
{
std::cout << "parse err!" << std::endl;
}
}
boost解析xml C++
最新推荐文章于 2024-05-01 15:40:09 发布