我在Delphi和.Net C#测试项目中使用WebBrowser控件导航到本地测试XML文件并尝试将内容保存回XML文件在.Net DocumentCompleted
事件和德尔福onNavigateComple2
事件。如何从WebBrowser控件获取XML(RAW/SOURCE)
的问题是,我总是得到的HTML,将通过浏览器查看转化(检查我的输出:我保存的使用下面的代码)
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;
嗯,我已经试过几乎所有的东西,到处搜索,但到目前为止找不到有用的东西。使用下面的Delphi代码我会显示源代码的工作原理(这意味着源代码在某处),但是我不能使用它,因为它会播放一个对话框并且不容易获取数据并关闭对话框(在我的测试用例中我与我的XML内容的notepad.exe)
AWebBrowser.Document.QueryInterface(IOleCommandTarget, CmdTarget) ;
if CmdTarget <> nil then
try
CmdTarget.Exec(PtrGUID, HTMLID_VIEWSOURCE, 0, vaIn, vaOut) ;
finally
CmdTarget._Release;
end;
我也设法调用SAVE AS呼叫与XXX-HIDE-XXX标志,但它接缝了IE 5另存为对话框将显示(隐藏标志将被忽略)。
我也尝试从缓存(缓存API)获取XML数据,但在我的情况下,我不会得到任何东西,2.如果客户机器上的缓存被禁用? ;-)
InnerText或InnerHTML atc。不能使用,因为它们包含 - 和+字符并且不代表原始RAW数据(SOURCE)
仅供参考:我无法使用WebClient或Indy组件访问xml。我也不能作为代理服务器,因为在打开客户机器上的端口(比如说8080)的问题上,有特权的用户访问是痛苦的。
所以我在这里问你是否有任何想法如何解决我的问题?
在此先感谢, 干杯
输入:
<?xml version="1.0" encoding="UTF-8"?>
<test><data>xxxx</data></test>
输出:
<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"> </SPAN>
<SPAN class="m"><?</SPAN><SPAN class="pi">xml version="1.0" encoding="UTF-8" </SPAN><SPAN class="m">?></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"><</SPAN><SPAN class="t">test</SPAN><SPAN class="m">></SPAN></DIV>
<DIV><DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b"> </SPAN>
<SPAN class="m"><</SPAN><SPAN class="t">data</SPAN><SPAN class="m">></SPAN><SPAN class="tx">xxxx</SPAN><SPAN class="m"></</SPAN><SPAN class="t">data</SPAN><SPAN class="m">></SPAN>
</DIV></DIV>
<DIV><SPAN class="b"> </SPAN>
<SPAN class="m"></</SPAN><SPAN class="t">test</SPAN><SPAN class="m">></SPAN></DIV>
</DIV></DIV>
</BODY>
</HTML>
等待 - *为什么*你不能直接用WebClient或Indy下载文件?看起来这些应该比涉及巨大的UI控制更简单。 – 2011-05-26 17:08:51
好吧,这只是我在这里发布的一个示例。我写了那个测试xml /测试应用程序。真正的场景非常复杂,需要用户在浏览器中进行交互,并且在用户完成浏览器和用户之间的所有内容后,直到最终结果是一个XML文件,您无法控制哪里来自哪里!这就是为什么! – Gohlool 2011-05-26 21:10:33