2

私は Delphi 7 と OmniXML を使用しており、ドキュメントを作成しようとしています。DocumentElement を次のようにする必要があります。

<?xml version="1.0" encoding="UTF-8"?>

?しかし、最後の記号を追加する方法がわかりません。

私のコード:

var    
  xml: IXMLDocument;   
begin    
  xml := ConstructXMLDocument('?xml');    
  SetNodeAttr(xml.DocumentElement, 'version', '1.0');   
  SetNodeAttr(xml.DocumentElement, 'encoding', 'UTF-8');    
  XMLSaveToFile(xml, 'C:\Test1.xml', ofIndent);  
end;
4

1 に答える 1

6
<?xml version="1.0" encoding="UTF-8"?>

これはDocument Elementではありません。これは要素ではなく、代わりに処理命令であり、たまたま XML プロローグとしても知られる XML 宣言です。

XML 宣言の属性を指定するには、代わりにこれを使用します。

xmlDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"');

例えば:

{$APPTYPE CONSOLE}

uses
  OmniXML;

var
  XMLDoc: IXMLDocument;
  ProcessingInstruction: IXMLProcessingInstruction;
  DocumentElement: IXMLElement;
begin
  XMLDoc := CreateXMLDoc;
  ProcessingInstruction := XMLDoc.CreateProcessingInstruction('xml',
    'version="1.0" encoding="UTF-8"');
  DocumentElement := XMLDoc.CreateElement('foo');

  XMLDoc.DocumentElement := DocumentElement;
  XMLDoc.InsertBefore(ProcessingInstruction, DocumentElement);

  XMLDoc.Save('foo.xml', ofIndent);
end.
于 2017-01-09T15:59:52.083 に答える