2

I'm working with Delphi 7 and OmniXML and am trying to create a document and I need to have the DocumentElement be:

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

But I can't understand how to add the last ? sign.

My code:

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;
J...
  • 30,968
  • 6
  • 66
  • 143
SovereignSun
  • 184
  • 1
  • 10

1 Answers1

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

This is not the Document Element. It is not even an element, it is a Processing Instruction instead, and it happens to be the XML declaration, sometimes also known as the XML prolog.

To specify the attributes of the XML declaration, use this instead:

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

For example:

{$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.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490