如何在Delphi 7中使用MSXML 6.0创建TXML文档?

nev*_*ves 3 delphi msxml delphi-7 msxml6

Delphi 7发布时,MSXML 6.0不存在.是否可以将Delphi的TXML文档配置为使用MSXML 6.0而不是旧版本?

Fri*_*man 10

将以下代码添加到单元名称uMSXMLVersion或您选择的名称,并将其添加到您的项目使用

{----------------------------------------------------------------------------    
  Set Delphi's XMLDocument to use MSXML v6.0

  Usage:  Include unit in project "uses"-list and Delphi will automatically use
          MSXML v6.0 for TXmlDocument.
-----------------------------------------------------------------------------}
unit uMSXMLVersion;

interface

implementation

uses ActiveX, MSXML, MSXMLDOM;

function CreateDOMDocumentEx: IXMLDOMDocument;
const
  CLASS_DOMDocument60: TGUID = '{88D96A05-F192-11D4-A65F-0040963251E5}';
begin
  Result := nil;
  if CoCreateInstance(CLASS_DOMDocument60, nil, CLSCTX_INPROC_SERVER or
    CLSCTX_LOCAL_SERVER, IXMLDOMDocument, Result) <> S_OK then
  Result := CreateDOMDocument; //call the default implementation
end;

initialization
  MSXMLDOMDocumentCreate := CreateDOMDocumentEx;

end.
Run Code Online (Sandbox Code Playgroud)


Rem*_*eau 8

msxmldom.pas单元公开了一个公共MSXMLDOMDocumentCreate钩子,您可以为其分配自定义处理程序,例如:

uses
  ..., msxmldom;

const
  CLASS_DOMDocument60: TGUID = '{88D96A05-F192-11D4-A65F-0040963251E5}';

function CreateMSXML6Document: IXMLDOMDocument;
var
  Disp: IDispatch;
begin
  OleCheck(CoCreateInstance(CLASS_DOMDocument60, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IDispatch, Disp));
  Result := Disp as IXMLDOMDocument;
  if not Assigned(Result) then
    raise DOMException.Create('MSXML 6.0 Not Installed');
end;

initialization
  msxmldom.MSXMLDOMDocumentCreate := CreateMSXML6Document;
Run Code Online (Sandbox Code Playgroud)