jaynaiPhone
jaynaiPhone

Reputation:

How do I read and write XML in Cocoa Touch?

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?

Upvotes: 1

Views: 2717

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243146

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.

Upvotes: 3

h4xxr
h4xxr

Reputation: 11465

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!

Upvotes: 0

Related Questions