How do I read and write XML in Cocoa Touch?

前端 未结 2 621
情深已故
情深已故 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 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

    Object 1ContentsOfObject1
    Object 2ContentsOfObject2 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!

提交回复
热议问题