html的结构形式
<div>
<img>
<table>
<tr>
<td 地址>姓名
<td>
<span>
<span>
<span>
对于类似上面结构的几类处理方法:
1. 清空html.
BOOL ClearHtml() //清空html里面的内容
{
CComPtr<IHTMLDocument2> pDoc( NULL );
if( !SUCCEEDED( GetDHtmlDocument( &pDoc ) ) )
{
return FALSE;
}
CComPtr<IHTMLElement> pElemBody( NULL );
if( pDoc )
{
if( !SUCCEEDED( pDoc->get_body( &pElemBody ) ) )
{
return FALSE;
}
}
else
{
return FALSE;
}
CComQIPtr<IHTMLDOMNode> pNode = pElemBody;
if (pNode)
{
CComPtr<IHTMLDOMNode> pchildNode = NULL;
CComPtr<IHTMLDOMNode> pLchildNode = NULL;
while(SUCCEEDED(pNode->get_firstChild(&pchildNode)))
{
HRESULT hr = pNode->removeChild(pchildNode, &pLchildNode);
pchildNode = NULL;
pLchildNode = NULL;
if(FAILED(hr))
break;
}
}
return FALSE;
}
2. 删除指定记录
BOOL CWndHtmlList::DeleteCommRecord( BOOL bFlags )
{
//首先是删除界面显示
BSTR bstr;
CComPtr<IHTMLDocument2> pDoc( NULL );
IHTMLElement* pHtmlElement;
if( !SUCCEEDED( GetDHtmlDocument( &pDoc ) ) )
{
return FALSE;
}
CComPtr<IHTMLElement> pElemBody( NULL );
if( pDoc )
{
if( !SUCCEEDED( pDoc->get_body( &pElemBody ) ) )
{
return FALSE;
}
}
else
{
return FALSE;
}
CComQIPtr<IHTMLDOMNode> pNode = pElemBody;
if (pNode)
{
CComPtr<IHTMLDOMNode> pchildNode = NULL;
CComPtr<IHTMLDOMNode> pLchildNode = NULL;
CComPtr<IHTMLDOMNode> pItemNode = NULL;
HRESULT hr = pNode->get_firstChild( &pchildNode );
while( SUCCEEDED( hr ) )
{
IDispatch* pDispatch;
IHTMLElementCollection* pHtmlCollection = NULL;
CComPtr<IHTMLElement> spElement = NULL;
//div
pchildNode->QueryInterface( &spElement );
spElement->get_innerHTML( &bstr );
spElement->get_children( &pDispatch );
pDispatch->QueryInterface( IID_IHTMLElementCollection,( void **)&pHtmlCollection );
VARIANT va;
va.vt = VT_I4;///指明整型数据
va.lVal = 1; ///赋值
pHtmlCollection->item( va,va,&pDispatch );
pDispatch->QueryInterface( IID_IHTMLElement,(void **)&pHtmlElement );
//table
pHtmlElement->get_innerHTML( &bstr );
//获取下一个
pHtmlElement->get_children( &pDispatch );
pDispatch->QueryInterface( IID_IHTMLElementCollection,( void **)&pHtmlCollection );
va.vt = VT_I4;///指明整型数据
va.lVal = 0; ///赋值
pHtmlCollection->item( va,va,&pDispatch );
pDispatch->QueryInterface( IID_IHTMLElement,(void **)&pHtmlElement );
//tr
pHtmlElement->get_innerHTML( &bstr );
//获取下一个
pHtmlElement->get_children( &pDispatch );
pDispatch->QueryInterface( IID_IHTMLElementCollection,( void **)&pHtmlCollection );
va.vt = VT_I4;///指明整型数据
va.lVal = 0; ///赋值
pHtmlCollection->item( va,va,&pDispatch );
pDispatch->QueryInterface( IID_IHTMLElement,(void **)&pHtmlElement );
//td
pHtmlElement->get_innerHTML( &bstr );
//获取下一个
pHtmlElement->get_children( &pDispatch );
pDispatch->QueryInterface( IID_IHTMLElementCollection,( void **)&pHtmlCollection );
va.vt = VT_I4;///指明整型数据
va.lVal = 0; ///赋值
pHtmlCollection->item( va,va,&pDispatch );
pDispatch->QueryInterface( IID_IHTMLElement,(void **)&pHtmlElement );
//
pHtmlElement->get_innerHTML( &bstr );
CComVariant varint(NULL);
if( SUCCEEDED( pHtmlElement->getAttribute( L"itemData", 0, &varint ) ) )
{
RECORD* record = (RECORD*)(LONG_PTR)_wtol( varint.bstrVal );
if ( record->strRecordID == recTemp.strRecordID )
{
HRESULT hr = pNode->removeChild(pchildNode, &pLchildNode);
pchildNode = NULL;
pLchildNode = NULL;
if(FAILED(hr))
break;
break;
}
}
//注意下面这条语句
pItemNode = NULL;
hr = pchildNode->get_nextSibling( &pItemNode );
if ( pItemNode )
{
pchildNode = pItemNode;
}
else
{
break;
}
}//while
}
return TRUE;
}