本文共 5785 字,大约阅读时间需要 19 分钟。
#include#include #include #include #include "tinyxml.h"#pragma comment(lib, "tinyxml.lib")#pragma comment(lib, "tinyxmlSTL.lib")using namespace std;void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) // { int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0); //返回需要的unicode长度 WCHAR *wszANSI = new WCHAR[nLen+1]; memset(wszANSI, 0, nLen * 2 + 2); nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度 char *szANSI=new char[nLen + 1]; memset(szANSI, 0, nLen + 1); WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi strANSI = szANSI; delete wszANSI; delete szANSI; }void getXml(LPCTSTR url){ CFile file((TEXT("temp.xml")), CFile::modeCreate|CFile::modeWrite); CString content; CString data, temp; DWORD dwStatusCode; CInternetSession session(TEXT("HttpClient")); CHttpFile* pfile = (CHttpFile *)session.OpenURL(url); pfile -> QueryInfoStatusCode(dwStatusCode); if(dwStatusCode == HTTP_STATUS_OK) { while (pfile -> ReadString(data)) { temp += data; } } pfile -> Close(); delete pfile; session.Close(); ConvertUTF8ToANSI(temp, content); file.Write(content, content.GetLength()); file.Close();}void readXml(){ TiXmlDocument doc("temp.xml"); doc.LoadFile(); //doc.Print(); TiXmlElement* rootElement = doc.RootElement(); //xml TiXmlElement* dataElement = rootElement->FirstChildElement(); //data for (; dataElement != NULL; dataElement = dataElement->NextSiblingElement()) { TiXmlElement* Element = dataElement->FirstChildElement(); for (; Element != NULL; Element = Element->NextSiblingElement()) { string Type = Element->Value(); string Value = Element->GetText(); cout << Type << " : " << Value << endl; } }}int main(){ LPCTSTR str = TEXT("http://api.kuaidi100.com/api?id=a78e61062aabe452&com=yuantong&nu=9100493541&show=1"); getXml(str); readXml(); //system("del temp.xml"); //system("pause"); return 0;}
需要将返回的UTF-8数据转换为string,char等都支持的ansi
void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) // { int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0); //返回需要的unicode长度 WCHAR *wszANSI = new WCHAR[nLen+1]; memset(wszANSI, 0, nLen * 2 + 2); nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度 char *szANSI=new char[nLen + 1]; memset(szANSI, 0, nLen + 1); WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi strANSI = szANSI; delete wszANSI; delete szANSI; }
或者利用C++11
void ConvertUTF8ToANSI(){ auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8寻得一个不用解码的API); std::wifstream wfin("temp1.xml"); std::wstring wstr, content; wfin.imbue(LocUtf8); while(getline(wfin, wstr)) { content += wstr; } wfin.close(); system("del temp1.xml"); //std::wcout.imbue(std::locale("")); //std::wcout << content << std::endl; std::locale::global(std::locale("Chinese-simplified")); std::wofstream wfout("temp.xml"); wfout << content; wfout.close();}
http://api.ickd.cn/?id=102530&secret=dff7b12be1ae97433b2b57c74633a98d&com=shentong&nu=668456861017&type=xml
支持的常用的快递公司更多,果断换了。
#include#include #include #include #include #include