title: Cocoa - How To Format An XML File
body: <p>Is there a way that I can format an XML file in the proper way when creating it programmatically?</p>
  
  <p>For example, if I use this code to create a simple XML File:</p>
  
  <pre><code>NSXMLElement *fallback_driver = [[NSXMLElement alloc] initWithName:@"fallback-driver"];
  
  NSXMLElement *folder = [[NSXMLElement alloc] initWithName:@"folder"];
  [folder setStringValue:[ads_make objectValueOfSelectedItem]];
  NSXMLElement *filename =[[NSXMLElement alloc] initWithName:@"filename"];
  [filename setStringValue:ads_driver_name_selected];
  
  [fallback_driver addChild:folder];
  [fallback_driver addChild:filename];
  
  NSXMLElement* rootNode = [ads_user_printxml rootElement];
  [rootNode addChild:fallback_driver];
  </code></pre>
  
  <p>When this is run, I would like to output to be as per the commented section in the image below, not the actual XML (that is not commented).</p>
  
  <p><img src="https://i.imgur.com/qRlOs.png" alt="xml code"></p>
  
  <p>How I can format the XML file that way? Thanks!</p>
  
  <p>P.S. </p>
  
  <p>Thanks for the answer.. However, I would like to convert the NSXMLDocument that I have into NSData for saving...</p>
  
  <p>I am trying         </p>
  
  <pre><code>NSData *newData = [[ads_user_printxml XMLDataWithOptions:NSXMLNodePrettyPrint]XMLData];
  </code></pre>
  
  <p>however I am getting a warning that "'NSData' may not respond to '-XMLData', where before I added the XMLDataWithOptions it was working fine. I also tried the method 'XMLStringWithOptions' (as you stated - but figured that data was more appropriate), but the same warning was generated.</p>
  
  <p>Any ideas? Thanks a lot!</p>