views:

916

answers:

2

I want to create a file using Objective-C, which stores the data comes from XML. I also have to do basic functions of read and write into that file. How can I do this?

A: 

If you have some flexibility over the XML structure, you could look at using the in-built commands to load and save a dictionary or array from/to a file, such as:

NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:myFileName];

This will load in an XML file into a dictionary. It expects the file to be in the format

<key>Object 1</key><string>ContentsOfObject1</string>
<key>Object 2</key><string>ContentsOfObject2</string> etc

(You can also init an array in the same way. The file is simpler, basically just leaving out the "key" part).

Then, to save it you just use the following command:

[self.myArrayorMyDictionary writeToFile:fileFullName atomically:YES];

Hope that helps!

h4xxr
note that a plist (property list) is a TYPED XML format.
kent
+3  A: 

You can parse a custom schema using the NSXMLParser class. This is especially useful since the NSXMLDocument class unfortunately does not exist on the iPhone. Thankfully, NSXMLParser is pretty easy to use. I've written an RSS feed parser using NSXMLParser in under half an hour.

Dave DeLong