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;
<?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.