永发信息网

怎样通过IHTMLDocument2接口,遍历本地html文件中的元素

答案:1  悬赏:10  手机版
解决时间 2021-02-05 18:15
  • 提问者网友:送舟行
  • 2021-02-05 11:05
怎样通过IHTMLDocument2接口,遍历本地html文件中的元素
最佳答案
  • 五星知识达人网友:神也偏爱
  • 2021-02-05 11:25
已经解决了:
CString CTestDlg::ParseElementFromResponse(CString strResponse)
{
CString strRet("");
//declare our MSHTML variables and create a document
MSHTML::IHTMLDocument2Ptr pDoc;
MSHTML::IHTMLDocument3Ptr pDoc3;
MSHTML::IHTMLElementCollectionPtr pCollection;
MSHTML::IHTMLElementPtr pElement;

HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&pDoc);

//put the code into SAFEARRAY and write it into document
SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
VARIANT *param;
bstr_t bsData = (LPCTSTR)strResponse;
hr = SafeArrayAccessData(psa, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = (BSTR)bsData;

hr = pDoc->write(psa);
hr = pDoc->close();

SafeArrayDestroy(psa);

//I'll use IHTMLDocument3 to retrieve tags. Note it is available only in IE5+
//If you don't want to use it, u can just run through all tags in HTML
//(IHTMLDocument2->all property)
pDoc3 = pDoc;

//display HREF parameter of every link (A tag) in ListBox
pCollection = pDoc3->getElementsByTagName(L"input");
for(long i=0; ilength; i++)
{
pElement = pCollection->item(i, (long)0);
if(pElement != NULL)
{
//second parameter says that you want to get text inside attribute as is
strRet += (LPCTSTR)bstr_t(pElement->getAttribute("name", 2));
strRet += "=";
strRet += (LPCTSTR)bstr_t(pElement->getAttribute("value", 2));
strRet += ";";
}
}


return strRet;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯