How do I read and write XML in Cocoa Touch?

前端 未结 2 601
情深已故
情深已故 2020-12-12 03:34

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?

相关标签:
2条回答
  • 2020-12-12 03:58

    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.

    0 讨论(0)
  • 2020-12-12 04:02

    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!

    0 讨论(0)
提交回复
热议问题