3,055
社区成员
发帖
与我相关
我的任务
分享
IHTMLDocument2* pDoc;
BSTR tagName, tdtag, itext;
tdtag = SysAllocString( L"TD");
SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (CoInitializeEx( 0L, COINIT_MULTITHREADED) == S_OK)
__try{
if (CoCreateInstance( CLSID_HTMLDocument, 0L, CLSCTX_INPROC, IID_IHTMLDocument2, (void**)&pDoc) == S_OK)
__try{
VARIANT *param;
SafeArrayAccessData(psa, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = SysAllocString(
L"<!doctype html><html><head><title>None</title></head>\
<body>\
<table>\
<tr>\
<th>Header</th>\
<td>Text1</td>\
<td>Text2</td>\
</tr>\
</table>\
</body>\
</html>");
if ((pDoc->write( psa) != S_OK)&&(pDoc->close() != S_OK))
return 1;
IDispatch *all, *disp;
IHTMLElement *body, *item, *td;
IHTMLElementCollection *alls, *tds;
IHTMLElement2 *tbl2;
long alen, tdlen;
pDoc->get_body( &body);
body->get_all( &all);
body->Release();
body = 0L;
all->QueryInterface( IID_IHTMLElementCollection, (void**)&alls);
all->Release();
all = 0L;
alls->get_length( &alen);
VARIANT dummy;
dummy.vt = VT_I4;
for( int ai = 0; ai < alen; ai++)
{
dummy.intVal = ai;
alls->item( dummy, dummy, (IDispatch**)&disp);
if (disp)
{
disp->QueryInterface( IID_IHTMLElement, (void**)&item);
if (item){
disp->Release();
disp = 0L;
item->get_tagName( &tagName);
if (!lstrcmpW( tagName, L"TABLE")){//if the element is a table at the root
item->QueryInterface( IID_IHTMLElement2, (void**)&tbl2);
item->Release();
item = 0L;
if(tbl2){
tbl2->getElementsByTagName( tdtag, &tds);//get the tr
if (tds){
tds->get_length( &tdlen);
for (int tri = 0; tri < tdlen; tri++){
dummy.intVal = tri;
tds->item( dummy, dummy, &disp);
if( disp){
disp->QueryInterface( IID_IHTMLElement, (void**)&td);
if(td){
td->get_innerText( &itext);
wprintf( L"%s\r\n", (LPWSTR)itext);
SysFreeString( itext);
td->Release();
td = 0L;
}
disp->Release();
disp = 0L;
}else{
disp->Release();
disp = 0L;
}
}
tds->Release();
tds = 0L;
}
tbl2->Release();
tbl2 = 0L;
}else{
item->Release();
item = 0L;
}
}else{
item->Release();
item = 0L;
}
}else{
disp->Release();
disp = 0L;
}
}else{
disp->Release();
disp = 0L;
}
}
if (alls){
alls->Release();
alls = 0L;
}
}__finally
{
pDoc->Release();
pDoc = 0L;
}
}__finally
{
SafeArrayDestroy( psa);
SysFreeString( tdtag);
CoUninitialize();
}
return 0;