2012-07-24 90 views
1

我不知道如何使用Delphi從TWebBrowser ActiveX組件中檢索XML內容。使用TWebBrowser Delphi組件下載XML文件

如果結果是一個HTML,我可以使用:

iall := (WebBrowser1.Document AS IHTMLDocument2).body; 
while iall.parentElement <> nil do 
begin 
    iall := iall.parentElement; 
end; 
memo1.Text := iall.outerHTML; 

但URL返回一個XML文件,並返回。體無關。

以下代碼缺失部分是什麼?

var S:String; 
begin 
    WebBrowser1.Navigate('http://192.168.0.35:8000/api/'); 
    if Assigned(WebBrowser1.Document) then 
    begin 
    Doc := (WebBrowser1.Document as IHTMLDocument2); 
    XMLText := ??? 
    end; 
end; 

備註:我無法使用Indy HTTPClient組件,因爲程序將運行在不同的代理配置上,並且很難處理很多配置。

+0

使用IE控制這樣的任務確實是矯枉過正。 Windows WinINet API使用在OS/IE中定義的代理設置。所以你可以使用URLDownloadToFile: URLDownloadToFile(nil,'http://192.168.0.35:8000/api/','file.xml',0,nil); – 2012-07-25 11:01:27

+0

嗨,安東尼,在服務器上沒有file.xml。 http://192.168.0.35:8000/api/是一個請求,它可以組合XML。此外,我找不到URLDownloadToFile的單位。 – Josir 2012-07-25 17:53:12

+0

這裏是例子http://www.swissdelphicenter.ch/torry/showcode.php?id=412 – 2012-07-25 18:38:01

回答

4

這似乎爲本地XML文件的工作,並應與一個URL以及工作:

procedure TForm3.FormShow(Sender: TObject); 
begin 
    WebBrowser1.Navigate('file:///d:/temp/TestFile.xml'); 
end; 

procedure TForm3.WebBrowser1DocumentComplete(ASender: TObject; 
    const pDisp: IDispatch; const URL: OleVariant); 
var 
    HTML: IHtmlDocument2; 
    XMLText: String; 
begin 
    HTML := WebBrowser1.Document as IHTMLDocument2; 
    XMLText := HTML.body.outerText; 

    // Just for display purposes, obviously. 
    ShowMessage(XMLText); 
end;