我需要把浏览过的网页(XML页面)保存到本地,怎样编码呢?希望给出源码,谢谢。

解决方案 »

  1.   

    HRESULT          hr    = E_FAIL;
        IDispatch*       pDisp = NULL;
        IHTMLDocument2*  pDoc  = NULL;

        pDisp                  = GetHtmlDocument();   if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))
       {
           IPersistFile* pFile = NULL;
           if(SUCCEEDED(pDoc->QueryInterface(IID_IPersistFile,(void**)&pFile)))
           {
    LPCOLESTR file = L"c:\\test1.htm";
    pFile->Save(file,TRUE);
           }
       }
      

  2.   

    可是浏览的是XML的页面啊,怎么不行啊?得到的只是一个空的html头啊
      

  3.   

    http://www.vckbase.com/document/viewdoc.asp?id=345
      

  4.   

    void CAView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
    {
    IDispatchPtr   spDisp;
    HRESULT         hr;   hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
       // Is the IDispatch* passed to us for the top-level window ?
       if (pDisp == spDisp)
       {
       IHTMLDocument2Ptr spDoc;      // Get the active document
          spDoc = GetHtmlDocument();
          if ( spDoc )
          {
          IHTMLWindow2Ptr spWin;         // Get the top-level window
             spDisp = spDoc->Script;
             spWin = spDisp;
             if ( spWin )
             {
                // Get the document
                spDoc = spWin->document;
                if ( spDoc )
                {
                IDispatchExPtr spDispEx;               // Get the document's IDispatchEx
                   spDoc->QueryInterface( IID_IDispatchEx,
                                          (void**)&spDispEx );
                   if ( spDispEx )
                   {
                   _bstr_t   bstrName("XMLDocument");
                   DISPID dispid;                  // Get the XMLDocument expando property
                      spDispEx->GetDispID( bstrName,
                                           fdexNameCaseSensitive,
                                           &dispid );
                      if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
                      {
                      VARIANT var;
                      DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};                     // Get the XMLDocument value
                         hr = spDispEx->Invoke( dispid,
                                           IID_NULL,
                                           LOCALE_USER_DEFAULT,
                                           DISPATCH_PROPERTYGET,
                                           &dpNoArgs,
                                           &var,
                                           NULL,
                                           NULL );
                         if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
                         {
                         IXMLDOMDocument* pXMLDoc=NULL;                        // Get the IXMLDOMDocument interface
                            var.pdispVal->QueryInterface(
                                           IID_IXMLDOMDocument,
                                           (void**)&pXMLDoc );
                            VariantClear( &var );
                            if ( pXMLDoc )
                            {
                            // Get the root element
                            IXMLDOMElement* pXMLElem=NULL;                           pXMLDoc->get_documentElement( &pXMLElem );
                               if ( pXMLElem )
                               {
                               BSTR bstr;
                               USES_CONVERSION;                              // Get/display the tag name
                                  pXMLElem->get_tagName( &bstr );
                                  AfxMessageBox( OLE2T(bstr) );
                                  pXMLElem->Release();
                               }
                               pXMLDoc->Release();
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }
      

  5.   

    void CAView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
    {
    IDispatchPtr   spDisp;
    HRESULT         hr;   hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
       // Is the IDispatch* passed to us for the top-level window ?
       if (pDisp == spDisp)
       {
       IHTMLDocument2Ptr spDoc;      // Get the active document
          spDoc = GetHtmlDocument();
          if ( spDoc )
          {
          IHTMLWindow2Ptr spWin;         // Get the top-level window
             spDisp = spDoc->Script;
             spWin = spDisp;
             if ( spWin )
             {
                // Get the document
                spDoc = spWin->document;
                if ( spDoc )
                {
                IDispatchExPtr spDispEx;               // Get the document's IDispatchEx
                   spDoc->QueryInterface( IID_IDispatchEx,
                                          (void**)&spDispEx );
                   if ( spDispEx )
                   {
                   _bstr_t   bstrName("XMLDocument");
                   DISPID dispid;                  // Get the XMLDocument expando property
                      spDispEx->GetDispID( bstrName,
                                           fdexNameCaseSensitive,
                                           &dispid );
                      if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
                      {
                      VARIANT var;
                      DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};                     // Get the XMLDocument value
                         hr = spDispEx->Invoke( dispid,
                                           IID_NULL,
                                           LOCALE_USER_DEFAULT,
                                           DISPATCH_PROPERTYGET,
                                           &dpNoArgs,
                                           &var,
                                           NULL,
                                           NULL );
                         if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
                         {
                         IXMLDOMDocument* pXMLDoc=NULL;                        // Get the IXMLDOMDocument interface
                            var.pdispVal->QueryInterface(
                                           IID_IXMLDOMDocument,
                                           (void**)&pXMLDoc );
                            VariantClear( &var );
                            if ( pXMLDoc )
                            {
                            // Get the root element
                            IXMLDOMElement* pXMLElem=NULL;                           pXMLDoc->get_documentElement( &pXMLElem );
                               if ( pXMLElem )
                               {
                               BSTR bstr;
                               USES_CONVERSION;                              // Get/display the tag name
                                  pXMLElem->get_tagName( &bstr );
                                  AfxMessageBox( OLE2T(bstr) );
                                  pXMLElem->Release();
                               }
                               pXMLDoc->Release();
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }
      

  6.   

    DocumentComplete()我查了MSDN是ActiveX的接口函数哦,在HtmlView里面怎么加进去呢?
      

  7.   

    看看CHtmlView的源代码BEGIN_EVENTSINK_MAP(CHtmlView, CFormView)
    ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)END_EVENTSINK_MAP()void CHtmlView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
    {
    UNUSED_ALWAYS(pDisp);
    ASSERT(V_VT(URL) == VT_BSTR); CString str(V_BSTR(URL));
    OnDocumentComplete(str);
    }
    在你的类里面照抄就可以了
      

  8.   

    我不明白的是IHTMLDocument2Ptr和IXMLDOMElement在哪里定义的啊还要import什么文件吗?
      

  9.   

    实在不明白,这段代码到底怎么用啊,我把BEGIN_EVENTSINK_MAP那段抄进去了,不行哦,编译通不过,最好发给我工程看看好吗?谢谢了[email protected]
      

  10.   

    In C++, a connection can be accomplished in roughly the same way. Visual C++ Native Com Support is used here for the sake of brevity. Add references to Shdocvw.dll and Mshtml.dll to the project: #import <mshtml.dll> // Internet Explorer 4.0x
    #import <mshtml.tlb> // Internet Explorer 5
      

  11.   

    #import <mshtml.tlb> // Internet Explorer 5
    这一段我早就加进去了IXMLDOMElement可不是这里面定义的哦(我导入了这个tlb文件仔细看过)。BEGIN_EVENTSINK_MAP(CHtmlView, CFormView)
    ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)END_EVENTSINK_MAP()我把这一段抄进去,并加入了DocumentComplete函数,编译说未定义_GetSinkmap
      

  12.   

    我包含了
    #include "mshtml.h"
    #import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb
    编译时这一句
    // Get the top-level window
    spDisp = spDoc->Script;

    error C2039: 'Script' : is not a member of 'IHTMLDocument2'
    怎么回事啊?
      

  13.   

    我包含了
    #include "mshtml.h"
    #import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb
    编译时这一句
    // Get the top-level window
    spDisp = spDoc->Script;

    error C2039: 'Script' : is not a member of 'IHTMLDocument2'
    怎么回事啊?
      

  14.   

    忘记抄头文件了……去看看CHtmlVIew的定义头文件,应该有个Declare Event Sink的
      

  15.   

    IHTMLDocument::Script PropertyRetrieves an interface pointer that provides access to the scripting object via a handle to the script engine. SyntaxHRESULT IHTMLDocument::get_Script(IDispatch **p);
    spDoc是IHTMLDocument2Ptr吗?
      

  16.   

    能把工程发给我看看吗?谢谢了,解决了一定加分我这里问题很奇怪,spDoc后面打->后List Member里面有Scripts,编译却说error C2039: 'Script' : is not a member of 'IHTMLDocument2'IHTMLDocument2Ptr spDoc;照你的代码抄的,不会有错啊。还有IXMLDOMElement我也不知道怎么定义。
      

  17.   

    修改MFCIE示例的mfcieVw.cpp
    #include "stdafx.h"
    #include "mfcie.h"
    #include "MainFrm.h"#include "mfcieDoc.h"
    #include "mfcieVw.h"#pragma warning(disable : 4192)
    #pragma warning(disable : 4049)
    #pragma warning(disable : 4146)
    #import <mshtml.tlb>
    #import <msxml4.dll>
    #pragma warning(default: 4192)
    #pragma warning(default: 4049)
    #pragma warning(default: 4146)
    // Include Microsoft HIML definitions...#include <dispex.h>//You may derive a class from CComModule and use it if you want to override
    //something, but do not change the name of _Module
    #include <atlbase.h>//for atlcom.h
    extern CComModule _Module;//for atlbase.h
    #include <atlcom.h>//for OLE2T……
    END_MESSAGE_MAP()BEGIN_EVENTSINK_MAP(CMfcieView, CHtmlView)
    ON_EVENT(CMfcieView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)
    END_EVENTSINK_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMfcieView construction/destruction……
    //END OF MFC MEMORY LEAK BUG FIXvoid CMfcieView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
    {
    ASSERT(V_VT(URL) == VT_BSTR);
    IDispatchPtr   spDisp;
    HRESULT         hr;   hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
       // Is the IDispatch* passed to us for the top-level window ?
       if (pDisp == spDisp)
       {
      MSHTML::IHTMLDocument2Ptr spDoc;      // Get the active document
          spDoc = GetHtmlDocument();
          if ( spDoc )
          {
          MSHTML::IHTMLWindow2Ptr spWin;         // Get the top-level window
             spDisp = spDoc->Script;
             spWin = spDisp;
             if ( spWin )
             {
                // Get the document
                spDoc = spWin->document;
                if ( spDoc )
                {
                IDispatchExPtr spDispEx;               // Get the document's IDispatchEx
                   spDoc->QueryInterface( IID_IDispatchEx,
                                          (void**)&spDispEx );
                   if ( spDispEx )
                   {
                   _bstr_t   bstrName("XMLDocument");
                   DISPID dispid;                  // Get the XMLDocument expando property
                      spDispEx->GetDispID( bstrName,
                                           fdexNameCaseSensitive,
                                           &dispid );
                      if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
                      {
                      VARIANT var;
                      DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};                     // Get the XMLDocument value
                         hr = spDispEx->Invoke( dispid,
                                           IID_NULL,
                                           LOCALE_USER_DEFAULT,
                                           DISPATCH_PROPERTYGET,
                                           &dpNoArgs,
                                           &var,
                                           NULL,
                                           NULL );
                         if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
                         {
                         MSXML2::IXMLDOMDocument* pXMLDoc=NULL;                        // Get the IXMLDOMDocument interface
                            var.pdispVal->QueryInterface(
                                           IID_IXMLDOMDocument,
                                           (void**)&pXMLDoc );
                            VariantClear( &var );
                            if ( pXMLDoc )
                            {
                            // Get the root element
                            MSXML2::IXMLDOMElement* pXMLElem=NULL;                           pXMLDoc->get_documentElement( &pXMLElem );
                               if ( pXMLElem )
                               {
                               BSTR bstr;
                               USES_CONVERSION;                              // Get/display the tag name
                                  pXMLElem->get_tagName( &bstr );
                                  AfxMessageBox( OLE2T(bstr) );
                                  pXMLElem->Release();
                               }
                               pXMLDoc->Release();
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }
    //end new code 
      

  18.   

    头文件中添加成员函数
    void DocumentComplete(LPDISPATCH pDisp, VARIANT* URL);
      

  19.   

    又忘记抄DECLARE_EVENTSINK_MAP了
    ……
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    DECLARE_EVENTSINK_MAP()
    };