Load an XML file in Mac Os X using Cocoa: NSXMLDocument
Here another snippet on how to load an XML file using the NSXMLDocument class. Our goal is to show an “Open File” dialog box, chose an xml file, and put it in a data structure (NSXMLDocument)
Here’s the code:
The only “strange” thing suggested by the Apple’s example and reported here, is the double initialization of NSXMLDocument, with different parameters: the first one with NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA
, the second one with NSXMLDocumentTidyXML
. This is because we are trying to recover from parsing errors. NSXMLDocumentTidyXML
reformats the document before the parsing. Hopefully it can correct some error, but it modifies the original file (take a look at the documentation).
More information is in the “Introduction to Tree-Based XML Programming Guide for Cocoa” on the Apple Devveloper web site.