How to add first line and comment line in XML file use NSXMLDocument

466 Views Asked by At

I use NSXMLDocument to write XML file in MAC app . I want format XML file same bellow:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<P2Main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:schemas-Professional-Plug-in:P2:ClipMetadata:v3.1">
<Additional> 
<LastModifiedDate>-1</LastModifiedDate>
<FileSize>-1</FileSize>
</Additional>
   <ClipContent>
   <ClipName>DEFAULT</ClipName>
   <GlobalClipID>DEFAULT</GlobalClipID>
   </ClipContent>
     <!--Not Changeable-->

but i don't know how to add first line of XML file :

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

And I want to format it has comment:

<!--Not Changeable-->
2

There are 2 best solutions below

0
On
NSXMLDocument *doc = [[NSXMLDocument alloc]init];
[doc setVersion:@"1.0"];
[doc setCharacterEncoding:@"UTF-8"];

You will get

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
0
On

Try using NSXMLDocument's insertChild:atIndex: for your comment NSXMLNode, specifying index 0 for example. That should at least bring the comment up to just below the XML DTD line.