gpt4 book ai didi

delphi - 通过 TXMLDocument 访问 IXMLDOMDocument2?

转载 作者:行者123 更新时间:2023-12-03 15:23:34 25 4
gpt4 key购买 nike

我有一些使用 Delphi 的 TXMLDocument 类的工作代码,并使用 TransformNode 方法执行 XSLT 转换。

但是,我需要启用 XSLT Javascript 函数( <msxml:script> 标签)并且 - 经过多次谷歌搜索 - 这意味着我需要设置 AllowXsltScript IXMLDOMDocument2的属性(property)为真。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms760290(v=vs.85).aspx

我已经成功实现了这一点 - 但只能通过修改 Delphi 库函数的源代码 CreateDOMDocumentmsxmldom.pas .

function CreateDOMDocument: IXMLDOMDocument;
var doc :IXMLDOMDocument2;
begin

doc := TryObjectCreate([CLASS_DOMDocument60, CLASS_DOMDocument40, CLASS_DOMDocument30,
CLASS_DOMDocument26, msxml.CLASS_DOMDocument]) as IXMLDOMDocument2;
if not Assigned(doc) then
raise DOMException.Create(SMSDOMNotInstalled);
doc.setProperty('AllowXsltScript', true); // Allow XSLT scripts!!
Result := doc;
end;

显然这远不能令人满意 - 那么如何在不修改库代码的情况下访问 IXMLDOMDocument2 对象?

最佳答案

您可以通过 MSXMLDOMDocumentCreate 变量重写创建函数:

unit Unit27;

interface

uses
xmldoc, xmlintf, msxml, msxmldom, Forms, SysUtils,
ActiveX, ComObj, XmlDom, XmlConst,
Windows, Messages, Classes, Controls, StdCtrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function TryObjectCreate(const GuidList: array of TGuid): IUnknown;
var
I: Integer;
Status: HResult;
begin
Status := S_OK;
for I := Low(GuidList) to High(GuidList) do
begin
Status := CoCreateInstance(GuidList[I], nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IDispatch, Result);
if Status = S_OK then Exit;
end;
OleCheck(Status);
end;

function CreateDOMDocument2: IXMLDOMDocument;

var
Doc2 : IXMLDOMDocument2;

begin
Doc2 := TryObjectCreate([CLASS_DOMDocument60, CLASS_DOMDocument40, CLASS_DOMDocument30,
CLASS_DOMDocument26, msxml.CLASS_DOMDocument]) as IXMLDOMDocument2;
if not Assigned(Doc2) then
raise DOMException.Create(SMSDOMNotInstalled);
Doc2.setProperty('AllowXsltScript', true);
Result := Doc2;
end;


procedure TForm1.FormCreate(Sender: TObject);

var
Doc : IXMLDocument;

begin
Doc := TXMLDocument.Create(nil);
Doc.LoadFromFile('c:\temp\test.xml');
end;


initialization
MSXMLDOMDocumentCreate := CreateDOMDocument2;
end.

关于delphi - 通过 TXMLDocument 访问 IXMLDOMDocument2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17143805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com