加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 通过TXMLDocument访问IXMLDOMDocument2?

发布时间:2020-12-15 09:20:42 所属栏目:大数据 来源:网络整理
导读:我有一些使用Delphi的T XMLDocument类的工作代码,并使用TransformNode方法执行XSLT转换. 但是,我需要启用XSLT Javascript函数( msxml:script标记)和 – 经过大量谷歌搜索 – 这意味着我需要将IXMLDOMDocument2的AllowXsltScript属性设置为true. http://msdn
我有一些使用Delphi的T XMLDocument类的工作代码,并使用TransformNode方法执行XSLT转换.

但是,我需要启用XSLT Javascript函数(< msxml:script>标记)和 – 经过大量谷歌搜索 – 这意味着我需要将IXMLDOMDocument2的AllowXsltScript属性设置为true.

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

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

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变量覆盖create函数:

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,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:temptest.xml');
end;


initialization
 MSXMLDOMDocumentCreate := CreateDOMDocument2;
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读