天天看點

boost解析xml C++

```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;
	}
}