CComPtr<IHTMLDocument>pDoc;
CComPtr<IHTMLDocument2>pDoc2;
bstr=NULL;
CComPtr<IHTMLElement>pElement;
::OleInitialize(NULL);
HRESULT hr=pDoc.CoCreateInstance(__uuidof(HTMLDocument), NULL,
CLSCTX_INPROC_SERVER);
if(!SUCCEEDED(hr))
{
AfxMessageBox("無法創建HTML對象,請檢查是否安裝了MS XML運行庫!");
}
hr==pDoc->QueryInterface(IID_IHTMLDocument2,(LPVOID *)&pDoc2);
if(!SUCCEEDED(hr))
{
AfxMessageBox("派生IHTMLDocument2失败!");
}
pDoc2->get_body(&pElement);//获得的 pElement为空要不可以
pElement->put_innerHTML(bstr)//载入自己的内容bstr
CComPtr<IHTMLDocument2>pDoc2;
bstr=NULL;
CComPtr<IHTMLElement>pElement;
::OleInitialize(NULL);
HRESULT hr=pDoc.CoCreateInstance(__uuidof(HTMLDocument), NULL,
CLSCTX_INPROC_SERVER);
if(!SUCCEEDED(hr))
{
AfxMessageBox("無法創建HTML對象,請檢查是否安裝了MS XML運行庫!");
}
hr==pDoc->QueryInterface(IID_IHTMLDocument2,(LPVOID *)&pDoc2);
if(!SUCCEEDED(hr))
{
AfxMessageBox("派生IHTMLDocument2失败!");
}
pDoc2->get_body(&pElement);//获得的 pElement为空要不可以
pElement->put_innerHTML(bstr)//载入自己的内容bstr
你要实现的功能是?
你可以用正则表达式进行分析。
http://www.vckbase.com/document/viewdoc/?id=1446
此时,pDoc2指向的文档是空的,get_body肯定返回空元素。
这时,可以open打开一个空文档,然后通过write将字符串写入文档,最后close,关闭文档。
如下:
//open文档
CComVariant vName="_self";
CComVariant vEmpty;
hr=pDoc2->open(L"about:blank",vName,vEmpty,vEmpty,NULL);//构造参数,准备write
LPCWSTR szwTest=L"<p>这是一个测试</p>";
BSTR bstr = SysAllocString(szwTest);VARIANT *param;
SAFEARRAY *sfArray;// Creates a new one-dimensional array
sfArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);hr = SafeArrayAccessData(sfArray,(LPVOID*) & param);
param->vt = VT_BSTR;
param->bstrVal = bstr;
hr = SafeArrayUnaccessData(sfArray);//write文档
hr=pDoc2->write(sfArray);
//close文档
hr=pDoc2->close();///////////////以下检测是否写入成功。
pDoc2->get_body(&pElement);//
CComBSTR bstrTest;
pElement->get_innerHTML(&bstrTest);
=======================================
愤怒以愚蠢开始,以后悔告终。——毕达哥拉斯
CSDN助手 签名走马灯
#define INITGUID
#include <initguid.h>
#include <exdisp.h>
#include <shlguid.h>
#include <memory.h>
#include <shlobj.h> HRESULT hr; IWebBrowser2* pWebBrowser = NULL;
if (FAILED(OleInitialize(NULL)))
{
return;
} // Instantiate a browser
if (FAILED(hr = CoCreateInstance(CLSID_InternetExplorer,
NULL, CLSCTX_SERVER, IID_IWebBrowser2,
(LPVOID*)&pWebBrowser)))
{
goto Error;
} // Show the browser, and navigate to the special location
// represented by the pidl
hr = pWebBrowser->put_Visible(VARIANT_TRUE); IDispatch* pDocDisp = NULL;
hr = pWebBrowser->get_Document(&pDocDisp);
if (pDocDisp)
{
IHTMLDocument2* pDoc = NULL;
hr = pDocDisp->QueryInterface(IID_IHTMLDocument2, reinterpret_cast<void **>(&pDoc)); IHTMLElement* pElement = NULL;
hr = pDoc->get_body(&pElement);
CString strHtmlText = lpData;//这里传入你要解析的文本
BSTR bstrHTMLText = strHtmlText.AllocSysString();
hr = pElement ->put_outerHTML(&bstrHTMLText);