📄 xml.cpp
字号:
#include "StdAfx.h"
#include "xml.h"
#include "StdAfx.h"
#include <afxinet.h>
#import "MSXML4.dll"
#include "helper.h"
#include "Request.h"
#pragma comment(lib,"wsock32.lib")
inline BSTR ConvertStringToBSTR(const char* pSrc)
{
if(!pSrc) return NULL;
DWORD cwch;
BSTR wsOut(NULL);
if(cwch = ::MultiByteToWideChar(CP_ACP, 0, pSrc,
-1, NULL, 0))//get size minus NULL terminator
{
cwch--;
wsOut = ::SysAllocStringLen(NULL, cwch);
if(wsOut)
{
if(!::MultiByteToWideChar(CP_ACP,
0, pSrc, -1, wsOut, cwch))
{
if(ERROR_INSUFFICIENT_BUFFER == ::GetLastError())
return wsOut;
::SysFreeString(wsOut);//must clean up
wsOut = NULL;
}
}
};
return wsOut;
};
void GetUTF8String(char** utf8, const char* value)
{
int length = strlen(value);
wchar_t* pWbuf = new wchar_t[length + 1];
MultiByteToWideChar(0, 0, value, length + 1, pWbuf, length + 1);
int newLength = WideCharToMultiByte(CP_UTF8, 0, pWbuf, -1, *utf8, 0,NULL,NULL);
*utf8 = new char[newLength+1];
WideCharToMultiByte(CP_UTF8, 0, pWbuf, -1, *utf8, newLength+1,NULL,NULL);
delete[] pWbuf;
}
// void GetMBCSString(char** mbcs, const char* value)
// {
// if(*mbcs != NULL)
// delete[] *mbcs;
// int length = strlen(value);
// wchar_t* pWbuf = new wchar_t[length + 1];
// MultiByteToWideChar(CP_UTF8, 0, value, length + 1, pWbuf, length + 1);
// int newLength = WideCharToMultiByte(CP_UTF8, 0, pWbuf, -1, *mbcs, 0,NULL,NULL);
// *mbcs = new char[newLength+1];
// WideCharToMultiByte(0, 0, pWbuf, -1, *mbcs, newLength+1,NULL,NULL);
// delete[] pWbuf;
// }
void GetWCSString(wchar_t** wcs, const char* value)
{
if (*wcs != NULL)
delete[] *wcs;
int length = strlen(value);
*wcs = new wchar_t[length + 1];
MultiByteToWideChar(CP_ACP, 0, value, length + 1, *wcs, length + 1);
}
enum Encoding
{
UNKNOWN=-1,
ANSI,
UTF8,
UNICODE,
UNICODEBigEnding
};
CString GeHttptFileByproxy(const char *url,char* username, char* password)
{
CString szContent;
char strProxyList[MAX_PATH];
strcpy(strProxyList, "10.0.1.24:8000");
// strcpy(strUsername, "");
// strcpy(strPassword, "");
DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
CString szServer, szObject;
INTERNET_PORT nPort;
AfxParseURL(url, dwServiceType, szServer, szObject, nPort);
CInternetSession mysession("Test");
CHttpConnection* pConnection;
CHttpFile* pHttpFile;
pConnection = mysession.GetHttpConnection(szServer,
INTERNET_FLAG_KEEP_CONNECTION,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL);
pHttpFile = pConnection->OpenRequest("GET", szObject,
NULL, 0, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION);
//here for proxy
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = strProxyList;
proxyinfo.lpszProxyBypass = NULL;
try{
mysession.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
pHttpFile->SetOption(INTERNET_OPTION_PROXY_USERNAME, username, strlen(username)+1);
pHttpFile->SetOption(INTERNET_OPTION_PROXY_PASSWORD, password, strlen(password)+1);
pHttpFile->SendRequest(NULL);
}catch (CException& e) {
MessageBox(NULL,"网络异常!",NULL,NULL);
}
//read file
CString rbuf;
CString str="";
while(pHttpFile->ReadString(rbuf))
str+=rbuf;
str.Replace("utf-8","utf-16");
pHttpFile->Close();
delete pHttpFile;
pConnection->Close();
delete pConnection;
mysession.Close();
return str;
}
void GetMBCSString(char** mbcs, const char* value)
{
if(*mbcs != NULL)
delete[] *mbcs;
int length = strlen(value);
wchar_t* pWbuf = new wchar_t[length + 1];
MultiByteToWideChar(CP_UTF8, 0, value, length + 1, pWbuf, length + 1);
int newLength = WideCharToMultiByte(CP_ACP, 0, pWbuf, -1, *mbcs, 0,NULL,NULL);
*mbcs = new char[newLength+1];
WideCharToMultiByte(0, 0, pWbuf, -1, *mbcs, newLength+1,NULL,NULL);
delete[] pWbuf;
}
MSXML2::IXMLDOMNodePtr findNode(MSXML2::IXMLDOMNodeListPtr pNodelist,_bstr_t str)
{
_bstr_t text="null";
for (int i=0;i<pNodelist->Getlength();i++)
{
text=pNodelist->item[i]->GetnodeName();
if(text==str)
return pNodelist->item[i];
}
return NULL;
}
ItemList* parseXML(const char* pbuf,CString& name)
{
ItemList* item=NULL;
ItemList* rear=NULL;
ItemList* newItem=NULL;
_bstr_t text="null";
_bstr_t bstrTitle("title");
_bstr_t bstrChannel("channel");
_bstr_t bstrItem("item");
_bstr_t bstrDescription("description");
_bstr_t bstrLink("link");
_bstr_t bstrAuthor("author");
_bstr_t bstrDate("pubDate");
MSXML2::IXMLDOMDocument2Ptr pDoc=NULL;
MSXML2::IXMLDOMElementPtr pRoot=NULL;
HRESULT hr;
hr= CoInitialize(NULL);
hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument26));
if(!SUCCEEDED(hr))
{
printf("fail!");
}
pDoc->put_async(VARIANT_FALSE);
pDoc->put_validateOnParse(VARIANT_FALSE);
pDoc->put_resolveExternals(VARIANT_FALSE);
pDoc->put_preserveWhiteSpace(VARIANT_TRUE);
//pDoc->createProcessingInstruction(_bstr_t(_T("xml")),_bstr_t(_T("version='1.0' encoding='gbk'")));
int length = strlen(pbuf);
_bstr_t bstr = ConvertStringToBSTR(pbuf);
try{
pDoc->loadXML(bstr);
}
catch (...) {
return NULL;
}
//pDoc->load(_variant_t("C://home28.xml"));
pRoot=pDoc->documentElement;
if (pRoot==NULL)
{
return NULL;
}
MSXML2::IXMLDOMNodeListPtr pRssPtr=pRoot->childNodes;
if(findNode(pRssPtr,bstrChannel)==NULL)
MessageBox(NULL,"格式错误!",NULL,MB_OK);
else{
MSXML2::IXMLDOMNodePtr pChannel=findNode(pRssPtr,bstrChannel);
if(!pChannel->hasChildNodes())
MessageBox(NULL,"格式错误!",NULL,MB_OK);
else
{
MSXML2::IXMLDOMNodeListPtr pChannelPtr=pChannel->childNodes;
if(findNode(pChannelPtr,bstrTitle)==NULL)
{
name="null";
}
else
{
name=_com_util::ConvertBSTRToString(findNode(pChannelPtr,bstrTitle)->Gettext());
}
if(findNode(pChannelPtr,bstrItem)==NULL)
{
}
else
{
for(int i=0;i<pChannelPtr->Getlength();i++)
{
if(pChannelPtr->item[i]->GetnodeName()==bstrItem)
{
MSXML2::IXMLDOMNodePtr pItem=pChannelPtr->item[i];
MSXML2::IXMLDOMNodeListPtr pItemlist=pItem->childNodes;
if(pItem->hasChildNodes())
{
MSXML2::IXMLDOMNodePtr ptr = findNode(pItemlist,bstrTitle);
char* title = new char[1];
title[0]='\0';
char* author = new char[1];
author[0] = '\0';
char* link = new char[1];
link[0] = '\0';
char* description = new char[1];
description[0] = '\0';
char* date = new char[20];
sprintf(date,"0000/00/00 00:00:00");
if (ptr)
{
_bstr_t b = ptr->Gettext();
delete[] title;
title =_com_util::ConvertBSTRToString(ptr->Gettext());
}
ptr = findNode(pItemlist,bstrAuthor);
if(ptr)
{
delete[] author;
author=_com_util::ConvertBSTRToString(ptr->Gettext());
}
ptr = findNode(pItemlist,bstrLink);
if(ptr)
{
delete[] link;
link=_com_util::ConvertBSTRToString(ptr->Gettext());}
ptr = findNode(pItemlist,bstrDescription);
if(ptr)
{
delete[] description;
_bstr_t b = ptr->Gettext();
description=_com_util::ConvertBSTRToString(b);
}
ptr = findNode(pItemlist,bstrDate);
if(ptr)
{
delete[] date;
date=_com_util::ConvertBSTRToString(ptr->Gettext());
}
if(item==NULL)
{
DateTime d(date);
char *c = d.toString();
item=new ItemList(link,title,author,description,d.toString(),FALSE,ItemList::nextID++);
delete[] c;
rear=newItem=item;
}
else{
DateTime d(date);
char *c = d.toString();
newItem=new ItemList(link,title,author,description,c,FALSE,ItemList::nextID++);
delete[] c;
rear->nextItem=newItem;
rear=newItem;
rear->nextItem=NULL;
newItem=NULL;
//delete d;
}
delete[] date;
delete[] description;
delete[] author;
delete[] title;
delete[] link;
}
}
}
}
}
}
return item;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -