如何从WebBrowser控件XML(RAW / SOURCE) [英] How to get XML (RAW/SOURCE) from a WebBrowser Control

查看:286
本文介绍了如何从WebBrowser控件XML(RAW / SOURCE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我既德尔福和.Net C#测试项目WebBrowser控件导航到本地测试XML文件,并试图将内容保存回一个XML文件中的.Net DocumentCompleted 事件及德尔福 onNavigateComple2 事件。

问题是,我总是得到的HTML,将通过浏览器观看转化(检查我的输出:我保存的使用以下code)

 程序TForm1.SaveHTMLSourceToFile(const的文件名:字符串;
  WB:TWebBrowser);
VAR
  PersistStream:的IPersistStreamInit;
  的FileStream:TFileStream的;
  流:IStream的;
  SaveResult:HRESULT;
开始
  PersistStream:= WB.Document为的IPersistStreamInit;
  的FileStream:= TFileStream.Create(文件名,fmCreate);
  尝试
    流:= TStreamAdapter.Create(的FileStream,soReference)为IStream的;
    SaveResult:= PersistStream.Save(流,真);
    如果失败(SaveResult),然后
      的MessageBox(手柄,没有保存源','错误',0);
  最后
    FileStream.Free;
  结束;
结束;

好吧,我已经试过几乎所有的东西,到处找但迄今为止没有找到有用的anyhting。用下面的德尔福code我manged展示其作品的来源(这意味着源是那里的地方),但我不能用这个,因为这将播下一个对话框,不容易得到的数据,并关闭该对话框(在我的测试情况下,我得到我的XML内容的notepad.exe)

  AWebBrowser.Document.QueryInterface(IOleCommandTarget,CmdTarget);
  如果CmdTarget<>然后零
  尝试
    CmdTarget.Exec(PtrGUID,HTMLID_VIEWSOURCE,0,虚荣,VAOUT);
  最后
    CmdTarget._Release;
  结束;

我也设法调用保存的呼叫与XXX-HIDE-XXX标志,但它接缝了IE 5另存为对话框将显示(Hide标志将被忽略)。

我也试图从快取(Cache API),但在我的情况下,我不会得到任何东西,2。如果客户机上什么禁用缓存获取XML数据? ; - )

的InnerText或innerHTML的ATC。不能使用,因为它们包含 - 和+炭和未重新presenting所述orignial RAW数据(源)

只是为了您的信息:没有办法,我使用Web客户端或Indy组件访问XML。我也不能发挥,因为与开放端口的问题代理(让说8080)客户机上的痛苦与特权用户访问。

所以我在这里并询问您,如果您有任何想法如何解决我的问题?

在此先感谢,
干杯

输入:

 <?XML版本=1.0编码=UTF-8&GT?;
<试验><数据> XXXX< /数据>< /试验>

输出:

 < HTML>< HEAD>
<样式和GT; BODY {字体:X-小'宋体';保证金右:1.5em}
.C {光标:手}
.B {颜色:红色; FONT-FAMILY:'宋体'; FONT-重量:大胆;文本装饰:无}
.E {保证金左:1EM; TEXT-INDENT:-1em;保证金右:1EM}
.K {保证金左:1EM; TEXT-INDENT:-1em;保证金右:1EM}
.T {颜色:#990000}
.xt {颜色:#990099}
.ns {颜色:红}
.DT {颜色:绿色}
.M {颜色:蓝}
.tx {字体重量:大胆}
.db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px固体#CCCCCC;字体:小快递}
.di {字体:小快递}
.D {颜色:蓝}
.pi {颜色:蓝}
.CB {TEXT-INDENT:0像素;保证金左:1EM;的margin-top:0像素;保证金底:0像素;填充左:.3em;字体:小快递;颜色:#888888}
.CI {字体:小快递;颜色:#888888}
pre {保证金:0像素;显示:内联}< / STYLE>
< SCRIPT><! -
函数f(E){
如果(e.className ==CI){如果(e.children(0).innerText.indexOf(\\ n)大于0)定位(即,CB);}
如果(e.className ==嘀){如果(e.children(0).innerText.indexOf(\\ n)大于0)定位(即,DB);}
e.id =;
}
功能定位(E,CL){
e.className = CL;
e.style.display =块;
J = e.parentElement.children(0);
j.className =C;
K = j.children(0);
k.style.visibility =看得见;
k.href =#;
}
功能CH(E){
标记= e.children(0)。儿童(0);
如果(mark.innerText ==+){
mark.innerText = - ;
对于(VAR I = 1; I< e.children.length;我++)
e.children(我).style.display =块;
}
否则如果(mark.innerText == - ){
mark.innerText =+;
对于(VAR I = 1; I< e.children.length;我++)
e.children(我).style.display =无;
}}
功能CH2(五){
标记= e.children(0)。儿童(0);
内容= e.children(1);
如果(mark.innerText ==+){
mark.innerText = - ;
如果(contents.className ==DB|| contents.className ==CB)
contents.style.display =块;
否则contents.style.display =内联;
}
否则如果(mark.innerText == - ){
mark.innerText =+;
contents.style.display =无;
}}
函数CL(){
E = window.event.srcElement;
如果(e.className =C!){E = e.parentElement;如果(!e.className =C){回报;}}
E = e.parentElement;
如果(e.className ==E)CH(E);
如果(e.className ==K)CH 2(E);
}
功能前(){}
函数h(){对window.status =;}
document.onclick = CL;
- >< / SCRIPT>
< / HEAD>
< BODY类=ST>< D​​IV CLASS =E>
&所述; SPAN类=b的>&放大器; NBSP;&下; / SPAN>
&所述; SPAN类=米>&放大器;所述;?&下; / SPAN>&下; SPAN类=圆周> XML版本=1.0编码=UTF-8&下; / SPAN>&下; SPAN CLASS =M>&放大器; GT;< / SPAN>
< / DIV>
< D​​IV CLASS =E>
< D​​IV CLASS =CSTYLE =保证金左:1EM; TEXT-INDENT:-2em>< A HREF =#的onclick =返回false的onfocus =H()级= b> - < / A>
&所述; SPAN类=米>&放大器;所述;&下; / SPAN>&下; SPAN类=t的>试验&下; / SPAN>&下; SPAN类=米>&放大器; GT;&下; / SPAN>< / DIV>
< D​​IV>< D​​IV CLASS =E>< D​​IV STYLE =保证金左:1EM; TEXT-INDENT:-2em>
&所述; SPAN类=b的>&放大器; NBSP;&下; / SPAN>
&所述; SPAN类=米>&放大器;所述;&下; / SPAN>&下; SPAN类=t的>数据&下; / SPAN>&下; SPAN类=米>&放大器; GT;&下; / SPAN>&下; SPAN类=TX> XXXX&下; / SPAN>&下; SPAN类=米>&放大器;所述; /&下; / SPAN>&下; SPAN类=t的>数据&下; / SPAN>&下; SPAN类=米>&放大器; GT;&下; / SPAN>
< / DIV>< / DIV>
< D​​IV>< SPAN CLASS =B>&安培; NBSP;< / SPAN>
&所述; SPAN类=米>&放大器;所述; /&下; / SPAN>&下; SPAN类=t的>试验&下; / SPAN>&下; SPAN类=米>&放大器; GT;&下; / SPAN>< / DIV>
< / DIV>< / DIV>
< / BODY>
< / HTML>


解决方案

您可以做的TWebBrowser BeforeNavigate2事件的文件的影子下载。结果
通过影子,我的意思是使用一个程序,从其他库下载文件的同时TWebBrowser被下载。这样一来,就可以得到该文件而不会被TWebBrowser修改。

我写了一个测试应用程序和所有我不得不这样做的获取文件的内容是

 程序TForm1.WebBrowserBeforeNavigate2(发件人:TObject的;
  常量pDisp:IDispatch接口; VAR URL,旗,TargetFrameName,的PostData,
  头:OleVariant; VAR取消:WordBool);
开始
  HttpGetText(URL,Memo1.Lines);
结束;

该HttpGetText是从突触库 HTTP阻塞函数://www.ararat。 CZ /突触/ doku.php /启动

您也可以使用ICS,印,或TDownLoadURL。注意,TDownLoadURL不堵,我从来没有能够得到它的AfterDownload事件工作。

I am using the WebBrowser Control in my both Delphi and .Net C# test projects to navigate to a local test XML file and try to save the content back to a XML file in .Net DocumentCompleted Event and in Delphi onNavigateComple2 event.

The Problem is that I always get HTML which will be transformed by Browser for viewing (check my output: I saved that with using the following code)

procedure TForm1.SaveHTMLSourceToFile(const FileName: string;
  WB: TWebBrowser);
var
  PersistStream: IPersistStreamInit;
  FileStream: TFileStream;
  Stream: IStream;
  SaveResult: HRESULT;
begin
  PersistStream := WB.Document as IPersistStreamInit;
  FileStream := TFileStream.Create(FileName, fmCreate);
  try
    Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
    SaveResult := PersistStream.Save(Stream, True);
    if FAILED(SaveResult) then
      MessageBox(Handle, 'Fail to save source', 'Error', 0);
  finally
    FileStream.Free;
  end;
end;

Well, I've tried almost everything, searched everywhere but till now couldn't find anyhting useful. With the following Delphi Code I manged to show the SOURCE which works (That means the source is somewhere there) but I can not use this since it will sow a dialog and not easy to get the data and close that dialog (in my test case I get the notepad.exe with my xml content)

  AWebBrowser.Document.QueryInterface(IOleCommandTarget, CmdTarget) ;
  if CmdTarget <> nil then
  try
    CmdTarget.Exec(PtrGUID, HTMLID_VIEWSOURCE, 0, vaIn, vaOut) ;
  finally
    CmdTarget._Release;
  end;

I also managed to call the SAVE AS call with the xxx-HIDE-xxx Flag, but it seams up IE 5 the save as Dialog will be shown (the hide flag will be ignored).

I also tried to get the XML Data from Cache (Cache API) but in my case I won't get anything and 2. what if on customer machine the caching is disabled? ;-)

InnerText or InnerHTML atc. can not be used, since they contain - and + char and not representing the orignial RAW data (the SOURCE)

Just for your information: There is no way for me to use WebClient or Indy components to access the xml. I also can't play as a Proxy since the Problem with the opening the ports (let say 8080) on customers machine is painful with privileged user access.

So here I am and asking you if you have any idea how to solve my Problem?

Thanks in advance, Cheers

input:

<?xml version="1.0" encoding="UTF-8"?>
<test><data>xxxx</data></test>

output:

<HTML><HEAD>
<STYLE>BODY{font:x-small 'Verdana';margin-right:1.5em}
.c{cursor:hand}
.b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none}
.e{margin-left:1em;text-indent:-1em;margin-right:1em}
.k{margin-left:1em;text-indent:-1em;margin-right:1em}
.t{color:#990000}
.xt{color:#990099}
.ns{color:red}
.dt{color:green}
.m{color:blue}
.tx{font-weight:bold}
.db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier}
.di{font:small Courier}
.d{color:blue}
.pi{color:blue}
.cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888}
.ci{font:small Courier;color:#888888}
PRE{margin:0px;display:inline}</STYLE>
<SCRIPT><!--
function f(e){
if (e.className=="ci"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"cb");}
if (e.className=="di"){if (e.children(0).innerText.indexOf("\n")>0) fix(e,"db");}
e.id="";
}
function fix(e,cl){
e.className=cl;
e.style.display="block";
j=e.parentElement.children(0);
j.className="c";
k=j.children(0);
k.style.visibility="visible";
k.href="#";
}
function ch(e){
mark=e.children(0).children(0);
if (mark.innerText=="+"){
mark.innerText="-";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="block";
}
else if (mark.innerText=="-"){
mark.innerText="+";
for (var i=1;i<e.children.length;i++)
e.children(i).style.display="none";
}}
function ch2(e){
mark=e.children(0).children(0);
contents=e.children(1);
if (mark.innerText=="+"){
mark.innerText="-";
if (contents.className=="db"||contents.className=="cb")
contents.style.display="block";
else contents.style.display="inline";
}
else if (mark.innerText=="-"){
mark.innerText="+";
contents.style.display="none";
}}
function cl(){
e=window.event.srcElement;
if (e.className!="c"){e=e.parentElement;if (e.className!="c"){return;}}
e=e.parentElement;
if (e.className=="e") ch(e);
if (e.className=="k") ch2(e);
}
function ex(){}
function h(){window.status=" ";}
document.onclick=cl;
--></SCRIPT>
</HEAD>
<BODY class="st"><DIV class="e">
<SPAN class="b">&nbsp;</SPAN>
<SPAN class="m">&lt;?</SPAN><SPAN class="pi">xml version="1.0" encoding="UTF-8" </SPAN><SPAN class="m">?&gt;</SPAN>
</DIV>
<DIV class="e">
<DIV class="c" STYLE="margin-left:1em;text-indent:-2em"><A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<SPAN class="m">&lt;</SPAN><SPAN class="t">test</SPAN><SPAN class="m">&gt;</SPAN></DIV>
<DIV><DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">&nbsp;</SPAN>
<SPAN class="m">&lt;</SPAN><SPAN class="t">data</SPAN><SPAN class="m">&gt;</SPAN><SPAN class="tx">xxxx</SPAN><SPAN class="m">&lt;/</SPAN><SPAN class="t">data</SPAN><SPAN class="m">&gt;</SPAN>
</DIV></DIV>
<DIV><SPAN class="b">&nbsp;</SPAN>
<SPAN class="m">&lt;/</SPAN><SPAN class="t">test</SPAN><SPAN class="m">&gt;</SPAN></DIV>
</DIV></DIV>
</BODY>
</HTML>

解决方案

You could do a "shadow" download of the file in the TWebBrowser BeforeNavigate2 event.
By shadow, I mean use a procedure from another library to download the file at the same time TWebBrowser is downloading it. This way, you can get the file without it being modified by TWebBrowser.

I wrote a test application and all I had to do the get the file contents is

procedure TForm1.WebBrowserBeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  HttpGetText(URL,Memo1.Lines);
end;

The HttpGetText is a blocking function from the Synapse library http://www.ararat.cz/synapse/doku.php/start

You could also use ICS, Indy, or TDownLoadURL. Note, TDownLoadURL is not blocking and I was never able to get its AfterDownload event to work.

这篇关于如何从WebBrowser控件XML(RAW / SOURCE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆