请问如何获得TWebBrowser得到的html内容

allenWGQ 2004-09-14 04:48:57
我用TWebBrowser组件实现http请求,返回的是XML内容,那么它会显示在浏览器中,那么我如何得到返回来的内容呢?想和很长时间了,也没有结果,多谢各位的回答
...全文
242 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2004-09-15
  • 打赏
  • 举报
回复
Straight to XML
Dear Web Team:
Hi, I have a Microsoft Visual Basic application that uses the Microsoft Internet Explorer WebBrowser control (Microsoft Internet Controls). When I navigate to an XML page and view the source I can view the original XML, but when I access the document object, I get HTML.

How do I access the original XML from the WebBrowser control?

Umashankar

The Web Team replies:
What you're seeing here is the default behavior of Internet Explorer. When you navigate to an XML document, Internet Explorer transforms the contents using the default XSL style sheet. When accessing the document object that is part of the Dynamic HTML object model, your application will receive an object representing the generated HTML.

Conveniently, as well as transforming your XML into a collapsible hierarchy display, Internet Explorer also adds an XMLDocument expando property to the document object that provides access to the original XML. See the MSDN Online reference for more information.

Expando properties enable a Dynamic HTML author to add properties to an existing object, thereby extending the object model, and are commonly used by script programmers to store additional information on HTML elements and objects. This property is available to your Visual Basic® application, as shown below.

Private Sub Form_Load()
WebBrowser1.navigate " test.xml"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WebBrowser1.Object) Then
Dim doc As Object
Set doc = WebBrowser1.document.XMLDocument
MsgBox doc.documentElement.childNodes.length
End If
End Sub

If you're a C++ programmer, you don't have the convenience that Visual Basic offers. If you want to access the XMLDocument expando property from a C++ application, you can do this directly by using the IDispatchEx interface. As you probably already know, the IDispatch interface provides support for exposing object methods and properties and is used extensively by Internet Explorer to provide a rich object model. The IDispatchEx interface extends IDispatch by providing features that allow an object model to be dynamic. It is this interface that provides the powerful dynamic language features offered by Microsoft scripting languages, including expando properties.

The following code demonstrates how a C++ programmer would access the XMLDocument expando property in a Microsoft Foundation Class (MFC) application that uses a CHtmlView-derived view class. Note that the sample code makes use of Microsoft Visual C++ COM complier support and that DocumentComplete is an overridden function.

void CAView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
IDispatchPtr spDisp;
HRESULT hr;

hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
// Is the IDispatch* passed to us for the top-level window ?
if (pDisp == spDisp)
{
IHTMLDocument2Ptr spDoc;

// Get the active document
spDoc = GetHtmlDocument();
if ( spDoc )
{
IHTMLWindow2Ptr spWin;

// Get the top-level window
spDisp = spDoc->Script;
spWin = spDisp;
if ( spWin )
{
// Get the document
spDoc = spWin->document;
if ( spDoc )
{
IDispatchExPtr spDispEx;

// Get the document's IDispatchEx
spDoc->QueryInterface( IID_IDispatchEx,
(void**)&spDispEx );
if ( spDispEx )
{
_bstr_t bstrName("XMLDocument");
DISPID dispid;

// Get the XMLDocument expando property
spDispEx->GetDispID( bstrName,
fdexNameCaseSensitive,
&dispid );
if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
{
VARIANT var;
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};

// Get the XMLDocument value
hr = spDispEx->Invoke( dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dpNoArgs,
&var,
NULL,
NULL );
if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
{
IXMLDOMDocument* pXMLDoc=NULL;

// Get the IXMLDOMDocument interface
var.pdispVal->QueryInterface(
IID_IXMLDOMDocument,
(void**)&pXMLDoc );
VariantClear( &var );
if ( pXMLDoc )
{
// Get the root element
IXMLDOMElement* pXMLElem=NULL;

pXMLDoc->get_documentElement( &pXMLElem );
if ( pXMLElem )
{
BSTR bstr;
USES_CONVERSION;

// Get/display the tag name
pXMLElem->get_tagName( &bstr );
AfxMessageBox( OLE2T(bstr) );
pXMLElem->Release();
}
pXMLDoc->Release();
}
}
}
}
}
}
}
}
}

masterjames 2004-09-14
  • 打赏
  • 举报
回复
HtmlText : string
masterjames 2004-09-14
  • 打赏
  • 举报
回复
HtmlText := IHtmlDocument2(self.WebBrowser.Document).Body.OuterHtml ;

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧