如何使用 Delphi / ADOM (OpenXML) 设置 xml DOCTYPE?

How to set xml DOCTYPE with Delphi / ADOM (OpenXML)?

使用 Adom 4.3,使用

创建新文档
DI := TDomImplementation.Create;
Doc := TDomDocumentNS.Create( DI )

如何设置

<!DOCTYPE ...

生成的 XML 文档中的行? /不在保存文件时将此行插入字符串列表/

文档的Doctypedecl 属性是只读的,创建文档后实际上是NIL,那么我该如何实现呢?谢谢

您可以让 DOM 实现创建一个 IDOMDocumentType 的实例,您可以在创建文档时使用它:

program test;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Xml.adomxmldom,
  Xml.XmlDom,
  Xml.XmlDoc;

procedure Main;
var
  DomImpl: IDOMImplementation;
  Doc: TXMLDocument;
begin
  DomImpl := GetDOM(sAdom4XmlVendor);
  Doc := TXMLDocument.Create(nil);
  try
    Doc.DOMVendor := GetDOMVendor(sAdom4XmlVendor);
    Doc.DOMDocument := DomImpl.createDocument('http://www.w3.org/2000/svg', 'svg:svg',
      DomImpl.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN',
      'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'));
    Doc.SaveToFile(ChangeFileExt(ParamStr(0), '.xml'));
  finally
    Doc.Free;
  end;
end;

begin
  try
    Main;
  except
    on E: Exception do
    begin
      ExitCode := 1;
      Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
    end;
  end;
end.

以上代码产生以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg:svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg:svg/>