当前位置 : 主页 > 网页制作 > xml >

使用TWebBrowser在Vista上查看类似于IE的XML

来源:互联网 收集:自由互联 发布时间:2021-06-13
在XP上,如果我想在TWebBrowser中查看 XML,这段代码工作正常: uses ComObj, MSHTML, ActiveX;procedure DocumentFromString(ABrowser: TWebBrowser; const HTMLString: wideString);var v: OleVariant; HTMLDocument: IHTMLDocument2;begi
在XP上,如果我想在TWebBrowser中查看 XML,这段代码工作正常:

uses ComObj, MSHTML, ActiveX;

procedure DocumentFromString(ABrowser: TWebBrowser; const HTMLString: wideString);
var
  v: OleVariant;
  HTMLDocument: IHTMLDocument2;
begin
  if not Assigned(ABrowser.Document) then
  begin
    ABrowser.Navigate('about:blank');
    while ABrowser.ReadyState <> READYSTATE_COMPLETE do
    begin
      Application.ProcessMessages;
      Sleep(0);
    end;
  end;
  HTMLDocument := ABrowser.Document as IHTMLDocument2;
  v := VarArrayCreate([0, 0], varVariant);
  v[0] := HTMLString;
  HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
  HTMLDocument.Close;
end;

procedure WebBrowserXML(ABrowser: TWebBrowser; const XmlString: WideString);
var
  xml, xsl: OleVariant;
  HTMLString: WideString;
begin
  xml := CreateOleObject('Msxml2.DOMDocument');
  xml.async := False;
  xml.loadXML(XmlString);
  // Assert(xml.parseError.errorCode = 0);
  xsl := CreateOleObject('Msxml2.DOMDocument');
  xsl.validateOnParse := False;
  xsl.async := False;
  xsl.load('res://msxml.dll/defaultss.xsl');
  // Assert(xsl.parseError.errorCode = 0);
  HTMLString := xml.transformNode(xsl);
  ABrowser.HandleNeeded;
  DocumentFromString(ABrowser, HTMLString);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowserXML(WebBrowser1, '<xml><node>Hello</node></xml>');
end;

方法如下:XML使用XSLT(defaultss.xsl)进行转换,结果为HTML.

在Vista上,我在xml.transformNode(xsl)行中获得了一个exeption:

The stylesheet does not caontain a document element. The stylesheet
may be empty, or it may not be a well-formed XML document

我试图从像xsl.load(‘my.xsl’)这样的文件直接加载我自己的XSLT副本:

http://forums.asp.net/t/1141304.aspx?xslt+viewing+XML+like+that+of+IE

但我仍然得到XSLT无效的相同错误.

如何让这段代码在Vista上运行?

从评论到link我提供:

I have also found that after calling directly into
res://msxml#.dll/defaultss.xsl for years, this method no longer works
in Vista. I’ve messed with all sorts of security settings, but that
does not seem to be the issue. It looks like my only option is to
release my own copy of defaultss.xsl.

我似乎无法提供defaultss.xsl的有效“我自己的副本”.他们都失败了同样的异常错误.我能做什么?

load() documentation显示了使用此URL的示例:

res://msxml3.dll/xml/defaultss.xsl

如果您想在应用中嵌入XSLT作为资源,请确保使用引用应用中该资源的res:URL.有关该语法,请参阅MSDN的文档:

res Protocol

Syntax

06001

Tokens

sFile
Percent-encoded path and file name of the module that contains the resource.

sType
Optional. String or numerical resource type. This can be either a custom resource or one of the predefined resource types that are recognized by the FindResource function. If a numerical resource type is specified, the number of the identifier must follow a # character. If this parameter is not specified, the default resource type is RT_HTML or RT_FILE.

sID
String or numerical identifier of the resource. If a numerical identifier is specified, the actual number of the identifier, not the identifier itself, must follow a # character.

我刚刚检查了Windows 7,msxml3.dll确实有一个名为DEFAULTSS.XSL的XML资源,但msxml4.dll和msxml6.dll没有,并且没有msxml.dll文件.

正如MSDN所说,res:如果未指定资源类型,则默认为HTML或FILE,因此使用res://msxml3.dll/defaultss.xls将不起作用,因为XSLT资源类型是XML.因此,您需要使用res://msxml3.dll/xml/defaultss.xls.

网友评论