delphi FormatXMLData异常

wgx48brx  于 2023-03-29  发布在  其他
关注(0)|答案(1)|浏览(112)

我有这个SVG文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="100%" height="100%" viewBox="0 0 67 67" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;"><g id="icon"><path d="M64.583,8.333c0,-3.449 -2.8,-6.25 -6.25,-6.25l-50,0c-3.449,0 -6.25,2.801 -6.25,6.25l0,50c0,3.45 2.801,6.25 6.25,6.25l50,0c3.45,0 6.25,-2.8 6.25,-6.25l0,-50Z" style="fill:#ebebeb;fill-opacity:0;stroke:#0dcaf2;stroke-width:4.01px;"/><g><circle cx="46.657" cy="16.816" r="8.809" style="fill:#f3ca69;"/><path d="M59.761,54.246l-15.758,-21.165c-0.508,-0.683 -1.548,-0.681 -2.054,0.003l-7.002,9.485l-11.004,-14.732c-0.506,-0.679 -1.525,-0.679 -2.032,-0l-14.753,19.758c-0.163,0.219 -0.253,0.485 -0.253,0.758l0,10.307l52.856,0l0,-4.414Z" style="fill:#4ea17e;fill-rule:nonzero;"/></g></g></svg>

在 Delphi 11.3中,当我用下面的代码加载这个文件并尝试用FormatXMLData化它时,我得到一个异常:

var ThisXMLDoc := Xml.XMLDoc.TXmlDocument.Create(nil);
try
  ThisXMLDoc.LoadFromFile(EditSVGPath.Text);
  var FormattedSVGText := Xml.XMLDoc.FormatXMLData(ThisXMLDoc.XML.Text); // ExceptionMessage="Invalid node type", ExceptionName="EXMLDocError"
finally
  ThisXMLDoc.Free;
end;

如何修复此错误并正确格式化XML?
更新:在function CloneNodeToDoc中的Xml.XMLDoc.pas中发生错误,其中SourceNode.nodeType被评估:如果SourceNode.nodeType = ntElement or ntAttribute or ntText, ntCData, ntComment or ntEntityRef or ntProcessingInstr or ntDocFragment则SourceNode.nodeType有效,否则将引发异常。此函数由Xml.XMLDoc.pas中的function FormatXMLData调用,其中SourceNode参数作为(TXMLNode($A150E48) as IXMLNode)传递。(顺便说一句,SVG文件由Affinity Designer导出)。

cld4siwp

cld4siwp1#

为了重现这个问题,我在 Delphi 10.4(Windows 7,64位)中创建了一个新的VCL应用程序,代码如下:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

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

var
  Form1: TForm1;

implementation

uses
  Xml.XmlDoc;

{$R *.dfm}

const
  S =
    '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' +
    '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
    '<svg width="100%" height="100%" viewBox="0 0 67 67" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" '+'style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;">' +
    '<g id="icon">' +
    '<path d="M64.583,8.333c0,-3.449 -2.8,-6.25 -6.25,-6.25l-50,0c-3.449,0 -6.25,2.801 -6.25,6.25l0,50c0,3.45 2.801,6.25 6.25,6.25l50,0c3.45,0 6.25,-2.8 6.25,-6.25l0,-50Z" style="fill:#ebebeb;fill-opacity:0;stroke:#0dcaf2;stroke-width:4.01px;"/>' +
    '<g>' +
    '<circle cx="46.657" cy="16.816" r="8.809" style="fill:#f3ca69;"/>' +
    '<path d="M59.761,54.246l-15.758,-21.165c-0.508,-0.683 -1.548,-0.681 -2.054,0.003l-7.002,9.485l-11.004,-14.732c-0.506,-0.679 -1.525,-0.679 -2.032,-0l-14.753,19.758c-0.163,0.219 -0.253,0.485 -0.253,0.758l0,'+'10.307l52.856,0l0,-4.414Z" style="fill:#4ea17e;fill-rule:nonzero;"/>' +
    '</g>' +
    '</g>' +
    '</svg>';

procedure TForm1.FormCreate(Sender: TObject);
begin

  var ThisXMLDoc := Xml.XMLDoc.TXmlDocument.Create(nil);
  try
    ThisXMLDoc.LoadFromXML(S);
    var FormattedSVGText := Xml.XMLDoc.FormatXMLData(ThisXMLDoc.XML.Text);
    RichEdit1.Text := FormattedSVGText;
  finally
    ThisXMLDoc.Free;
  end;

end;

end.

实际上它失败了,但有EDOMParseError例外:DTD är förbjudet. 'DTD not allowed.'
如果删除DTD,

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

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

var
  Form1: TForm1;

implementation

uses
  Xml.XmlDoc;

{$R *.dfm}

const
  S =
    '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' +
//    '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
    '<svg width="100%" height="100%" viewBox="0 0 67 67" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" '+'style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;">' +
    '<g id="icon">' +
    '<path d="M64.583,8.333c0,-3.449 -2.8,-6.25 -6.25,-6.25l-50,0c-3.449,0 -6.25,2.801 -6.25,6.25l0,50c0,3.45 2.801,6.25 6.25,6.25l50,0c3.45,0 6.25,-2.8 6.25,-6.25l0,-50Z" style="fill:#ebebeb;fill-opacity:0;stroke:#0dcaf2;stroke-width:4.01px;"/>' +
    '<g>' +
    '<circle cx="46.657" cy="16.816" r="8.809" style="fill:#f3ca69;"/>' +
    '<path d="M59.761,54.246l-15.758,-21.165c-0.508,-0.683 -1.548,-0.681 -2.054,0.003l-7.002,9.485l-11.004,-14.732c-0.506,-0.679 -1.525,-0.679 -2.032,-0l-14.753,19.758c-0.163,0.219 -0.253,0.485 -0.253,0.758l0,'+'10.307l52.856,0l0,-4.414Z" style="fill:#4ea17e;fill-rule:nonzero;"/>' +
    '</g>' +
    '</g>' +
    '</svg>';

procedure TForm1.FormCreate(Sender: TObject);
begin

  var ThisXMLDoc := Xml.XMLDoc.TXmlDocument.Create(nil);
  try
    ThisXMLDoc.LoadFromXML(S);
    var FormattedSVGText := Xml.XMLDoc.FormatXMLData(ThisXMLDoc.XML.Text);
    RichEdit1.Text := FormattedSVGText;
  finally
    ThisXMLDoc.Free;
  end;

end;

end.

它按预期工作:

因此,您的问题似乎与您的问题中没有详细说明的情况有关,或者与Delphi10.4中没有的 Delphi 问题有关。(有和没有BOM),但它在UTF-16/32 LE/BE(有和没有BOM)时失败。然而,在这种情况下,错误消息确实明确地抱怨编码。

相关问题