我希望使用winHttp 系列API来 GET html的源码
然后将读取到的缓存写入到IHTMLDocument2中,然后通过IHTMLDocument2
来依DOM方式解析这个Buf请问缓存写入到IHTMLDocument2中来构建IHTMLDocument2对象应该如何操作
比如读取的源码内容在 string g_HtmlBuf中

解决方案 »

  1.   

    如何将内存中的html源码缓存利用DOM方式进行解析
      

  2.   

    #include <windows.h>  
    #include <mshtml.h>  
    OLECHAR szHTML[] = OLESTR("<HTML><BODY>Hello World!</BODY></HTML>");  
    int __stdcall WinMain(HINSTANCE hInst,  
                          HINSTANCE hPrev,  
                          LPSTR lpCmdLine,  
                          int nShowCmd)  
    {  
      IHTMLDocument2 *pDoc = NULL;  
      CoInitialize(NULL);  
      CoCreateInstance(CLSID_HTMLDocument,  
                       NULL,  
                       CLSCTX_INPROC_SERVER,  
                       IID_IHTMLDocument2,  
                       (LPVOID *) &pDoc);  
      if (pDoc)  
      {  
        IPersistStreamInit *pPersist = NULL;  
        pDoc->QueryInterface(IID_IPersistStreamInit,  
                           (LPVOID *) &pPersist);  
        if (pPersist)  
        {  
          IMarkupServices *pMS = NULL;  
          pPersist->InitNew();  
          pPersist->Release();  
          pDoc->QueryInterface(IID_IMarkupServices,  
                                  (LPVOID *) &pMS);  
          if (pMS)  
          {  
            IMarkupContainer *pMC = NULL;  
            IMarkupPointer *pMkStart = NULL;  
            IMarkupPointer *pMkFinish = NULL;  
            pMS->CreateMarkupPointer(&pMkStart);  
            pMS->CreateMarkupPointer(&pMkFinish);  
            pMS->ParseString(szHTML,  
                             0,  
                             &pMC,  
                             pMkStart,  
                             pMkFinish);  
            if (pMC)  
            {  
              IHTMLDocument2 *pNewDoc = NULL;  
              pMC->QueryInterface(IID_IHTMLDocument,  
                                  (LPVOID *) &pNewDoc);  
              if (pNewDoc)  
              {  
                // do anything with pNewDoc, in this case  
                // get the body innerText.  
                IHTMLElement *pBody;  
                pNewDoc-gt;get_body(&pBody);  
                if (pBody)  
                {  
                  BSTR strText;  
                  pBody->get_innerText(&strText);  
                  pBody->Release();  
                  SysFreeString(strText);  
                }  
                pNewDoc->Release();  
              }  
              pMC->Release();  
            }  
            if (pMkStart)  
                pMkStart->Release();  
            if (pMkFinish)  
              pMkFinish->Release();  
            pMS->Release();  
          }  
        }  
        pDoc->Release();  
      }  
      CoUninitialize();  
      return TRUE;  
    }