首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过IXMLDOMDocument2访问TXMLDocument?

通过IXMLDOMDocument2访问TXMLDocument?
EN

Stack Overflow用户
提问于 2013-06-17 08:57:42
回答 2查看 3K关注 0票数 4

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

但是,我需要启用XSLT函数(<msxml:script>标记),并且--在大量搜索之后--这意味着我需要将IXMLDOMDocument2AllowXsltScript属性设置为true。

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

我已经成功地实现了这一点--但只有通过修改Delphi函数CreateDOMDocument的源代码才能做到这一点。

代码语言:javascript
复制
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对象而不修改库代码呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-17 09:55:24

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

代码语言:javascript
复制
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.
票数 4
EN

Stack Overflow用户

发布于 2013-06-17 16:31:27

请注意,在XE3和更高版本中,不推荐使用MSXMLDOMDocumentCreate来子类TMSXMLDOMDocumentFactory并重写它的CreateDOMDocument函数。对于将来的参考,下面是XE3和XE4的一个示例:

代码语言:javascript
复制
interface

type
  TMSXMLDOMDocument2Factory = class(TMSXMLDOMDocumentFactory)
  public
    class function CreateDOMDocument: IXMLDOMDocument; override;
  end;

implementation

{ TMSXMLDOMDocument2Factory }

class function TMSXMLDOMDocument2Factory.CreateDOMDocument: IXMLDOMDocument;
begin
  Result := inherited;
  if not Assigned(Result) then
    raise DOMException.Create(SMSDOMNotInstalled);
  AddDOMProperty('AllowXsltScript', True);
  SetDOMProperties(Result as IXMLDOMDocument2);
end;

initialization
  MSXMLDOMDocumentFactory := TMSXMLDOMDocument2Factory;

end.
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17143805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档