xml 解析 无处不在,web编程时所用到的ajax 其它底层就是调用 的msxml.dll服务。
#include "stdafx.h"
#import "msxml6.dll"
using namespace MSXML2;
inline void TESTHR( HRESULT _hr )
{ if FAILED(_hr) throw(_hr); }
void XMLHttpRequestSample()
{
MSXML2::IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
BSTR bstrString = NULL;
HRESULT hr;
try {
// init
_bstr_t bType="Content-Type: application/xml\r\n";
_bstr_t bAgent="Client-Agent:PVI_P801_V0.10/800*600/other\r\n";
_bstr_t bCalling="x-up-calling-line-id:13466320945\r\n";
_bstr_t bAction="Action:register\r\n";
_bstr_t bAPI="APIVersion:1.0.0\r\n";
_bstr_t bUser="User-Agent: Mozilla/4.0\r\n";
_bstr_t bAccept="Accept:*/*\r\n";
_bstr_t bCache="Cache-Control:no-cache\r\n";
_variant_t varHeader =bType+bAgent+bCalling+bAction+bAPI+bUser+bAccept+bCache;
_bstr_t varHost="http://localhost:81/book.xml"
hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
SUCCEEDED(hr) ? 0 : throw hr;
hr=pIXMLHTTPRequest->open("GET", varHost, false);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pIXMLHTTPRequest->send(varHeader);
SUCCEEDED(hr) ? 0 : throw hr;
bstrString=pIXMLHTTPRequest->responseText;
// MessageBox(NULL, _bstr_t(bstrString), _T("Results"), MB_OK);
printf("**************************************************************************");
printf(_bstr_t(bstrString));
if(bstrString)
{
::SysFreeString(bstrString);
bstrString = NULL;
}
} catch (...) {
MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
if(bstrString)
::SysFreeString(bstrString);
}
// CoUninitialize();
}
void XMLDOMNodeSample()
{
try {
MSXML2::IXMLDOMDocumentPtr docPtr;
MSXML2::IXMLDOMNodePtr DOMNodePtr;
TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.6.0"));
VARIANT vtTemp;
vtTemp.vt=VT_I2;
vtTemp.iVal = 1; //NODE_ELEMENT
// load a document
_variant_t varXml("D:\\ebook_host\\book.xml");
_variant_t varOut((bool)TRUE);
varOut = docPtr->load(varXml);
if ((bool)varOut == FALSE)
throw(0);
MessageBox(NULL, _bstr_t(docPtr->xml), _T("Original Document"), MB_OK);
//DOMNodePtr = docPtr->createNode(vtTemp, "VIDEOS", ""); // 创建 新节点
//docPtr->documentElement->appendChild(DOMNodePtr); //添加 子节点
//MessageBox(NULL, _bstr_t(docPtr->xml), _T("New Document"), MB_OK);
MSXML2::IXMLDOMElementPtr pXMLDocElement = NULL;
pXMLDocElement = docPtr->documentElement;
MSXML2::IXMLDOMNodeListPtr pXMLDomNodeList = NULL;
pXMLDomNodeList = pXMLDocElement->selectNodes("descendant::person[name='ab']");
int count = 0;
count = pXMLDomNodeList->length;
for(int i=0;i<pXMLDomNodeList->length;i++)
{
DOMNodePtr=pXMLDomNodeList->Getitem(i);
if(DOMNodePtr!=NULL)
{
MessageBox(NULL, _bstr_t(DOMNodePtr->Gettext()), _T("New Document"), MB_OK);
}
}
} catch(...)
{
MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
}
}
int main()
{
// init
TESTHR(CoInitialize(NULL));
XMLHttpRequestSample();
XMLDOMNodeSample();
CoUninitialize();
return 0;
}