读书人

请教用XMLHttp的get_responseXML如何能

发布时间: 2012-03-18 13:55:39 作者: rapoo

请问用XMLHttp的get_responseXML怎么能拿到IXMLDOMDocument2Ptr
请问怎么用IXMLHTTPRequestPtr的get_responseXML获得IXMLDOMDocument2Ptr并把其中的XML文本内容显示出来?


我的代码如下:

IXMLHTTPRequestPtr xmlrequest;

LRESULT hr=xmlrequest.CreateInstance("Msxml2.XMLHTTP");
if(!SUCCEEDED(hr))
{
std::cout<<"无法创建XMLHTTP对象,请检查是否安装了MS XML运行库! "<<std::endl;
}

_variant_t varp(false);

xmlrequest->open(_bstr_t("POST"),_bstr_t

("http://192.168.1.81/Interface/CheckPrivilege.aspx"),varp); //varp参数表明方法是否异步

xmlrequest->send(_bstr_t

("<Level><LevelName>XXXX</LevelName><LevelPass>XXXXX</LevelPass></Level>"));

IXMLDOMDocument2Ptr pXMLDom;
hr=pXMLDom.CreateInstance(__uuidof(DOMDocument40));
if(FAILED(hr))
{
std::cout<<"Failed to instantiate DOMDocument40 class!"<<std::endl;
return -1;
}

xmlrequest->get_responseXML((IDispatch**)&pXMLDom);
std::cout<<static_cast<char*>(pXMLDom->xml)<<std::endl;


这样做什么内容都显示不出来,
不过如果我加上
BSTR bstrbody;
xmlrequest->get_responseText(&bstrbody);
pXMLDom->loadXML(bstrbody);
的话就能正常显示其中内容了





求助!非得费这二遍事么?如何直接用get_responseXML获得IXMLDOMDocument2Ptr并将其中内容显示出来?

[解决办法]

C/C++ code
#include <stdio.h>#include <iostream.h>// If neccessary, change the file path if your msxml.dll file is// in a different location.#import "c:\windows\system32\msxml.dll"// To use MSXML 4.0, import the dll msxml4.dll instead of msxml.dll, as follows:// #import "c:\windows\system32\msxml4.dll"// using namespace MSXML2;using namespace MSXML;int main(int argc, char* argv[]){   CoInitialize(NULL);   try   {      // Variables.      bstr_t sUrl = "http://Server/public/TestFolder1/test.eml";      bstr_t sMethod = "PROPFIND";      _variant_t vUser = L"Domain\\Username";      _variant_t vPassword = L"!Password";      _variant_t vAsync = (bool)FALSE;      bstr_t sReq;      long lStatus;      BSTR bstrResp;      BSTR bstrResponseText;      HRESULT hr;      // If you are using MSXML 2.0, use the following to initialize pXMLHttpReq:      MSXML::IXMLHttpRequestPtr pXMLHttpReq=NULL;      // If you are using MSXML 4.0, use the following to initialize pXMLHttpReq:      //MSXML2::IXMLHTTPRequestPtr pXMLHttpReq=NULL;      // Initialize the XMLHTTPRequest object pointer.      hr = pXMLHttpReq.CreateInstance(__uuidof(XMLHTTPRequest));      // Check the status of pointer creation.      if (S_OK != hr)      {         cout<< "XML Http Request pointer creation failed. << endl;         return 1;      }      // Open the XMLHTTPRequest object with the PROPFIND method and      // specify that it will be sent asynchronously.      pXMLHttpReq->open(sMethod,                        sUrl,                        vAsync,                        vUser,                        vPassword);      // Set the Content-Type header to "text/xml".      pXMLHttpReq->setRequestHeader((bstr_t)"Content-Type",                                    (bstr_t)"text/xml");      // Build the request body and search for the display name of the      // item.      sReq = "<?xml version='1.0'?>";      sReq = sReq +  "<d:propfind xmlns:d='DAV:'><d:prop>" +             "<d:displayname/></d:prop></d:propfind>";      // Send the PROPFIND method request.      pXMLHttpReq->send(sReq);      // Get the method request status.      pXMLHttpReq->get_status(&lStatus);      // An error occurred on the server.      if(lStatus >= 500)      {         pXMLHttpReq->get_statusText(&bstrResp);         cout << "Status: " << lStatus << endl              << "Status text: An error occurred on the server."          << endl              << (char*)(bstr_t)bstrResp              << endl;      }     // The method request was successful.     else if(lStatus == 207)     {        // Variables.        MSXML::IXMLDOMDocumentPtr pDOMDoc = NULL;        MSXML::IXMLDOMNodeListPtr pDOMNodeList = NULL;        MSXML::IXMLDOMNodePtr pDOMNode = NULL;        BSTR bstrText;        // Create an instance of the DOM Document.        HRESULT hr = pDOMDoc.CreateInstance(__uuidof(DOMDocument));        // Check the status of pointer creation.        if(FAILED(hr))           cout<<"Creation of DOMDocument failed."<<endl;        // Get the method response XML text.        pXMLHttpReq->get_responseText(&bstrText);        // Load the XML document with the response text.        pDOMDoc->loadXML(bstrText);        // Build a list of the DAV:displayname XML nodes (there should        // be only one) corresponding to the folders returned in the        // search request. The DAV: namespace is typically assigned        // the a: prefix in the XML response body.        pDOMNodeList = pDOMDoc->getElementsByTagName((bstr_t)"a:displayname");        // Display the display name of the resource.        pDOMNode = pDOMNodeList->nextNode();        cout << pDOMNode->text << endl;     }     else     {        // Display the response status.        cout << "Status: " << lStatus << endl;        // Display the response status text.        pXMLHttpReq->get_statusText(&bstrResp);        cout << "Status text: " << (char*)(bstr_t)bstrResp << endl;        // Display the response text.        pXMLHttpReq->get_responseText(&bstrResponseText);        cout << "Response text: " << (char*)(bstr_t)bstrResponseText << endl;     }   }   catch(_com_error &e)   {      // Display the error information.      cout << "Error code: " << (char*)e.Error() << endl           << "Error message: " << (char*)e.ErrorMessage()           <<endl;      return 1;   }   CoUninitialize();   return 0;} 

读书人网 >VC/MFC

热点推荐