What’s a good way to archive the data from a class into XML and write it out to a file? (in Objective-C for iPhone SDK)
I see lots about using NSXMLParser to parse the data but not much on creating the files.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you asked for easiest way… if you can get the data into an array or dictionary you can write it out as an XML plist with one line of code:
Dictionaries and arrays can contain other dictionaries and arrays as values which helps model nested hierarchies.
Since you can only write to the Documents folder at runtime you’ll want something like this to establish the path:
For custom marshalling of objects then you’ll want to look into
NSCodingand archives. Most of the XML libraries out there don’t handle generation so you’ll have to roll your own.Another option is to go the CoreData way and let it take care of the reading and writing for you. But it currently supports SQLite, binary, and in-memory persistent stores, so no XML just yet.