how to save data to XML file on iPhone
I want to save data to xml file , which include
- Create xml file
- insert in it
- delete from it
- update certain node
- read all the elements
any suggestion please , any sample tutor开发者_C百科ial code will be highly appreciated
NSURL *url = [NSURL URLWithString:@"http://abcd.com/sample.xml"];
NSData *data = [NSData dataWithContentsOfURL:url]; // Load XML data from web
// construct path within our documents directory
NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"sample.xml"];
// write to file atomically (using temp file)
[data writeToFile:storePath atomically:TRUE];
If you are parsing the XML into an NSArray or NSDictionary, it is better to just save the array or dictionary so that you don't have to parse the data in the cache every time you want to read it. Both NSArray and NSDictionary have the methods writeToFile and initWithContentsOfFile
You can use .plist file for XML structured storing the XML. It works for me.
Why do you want to use XML-files on the iPhone? There are more efficient ways to handle that data on the device. You might want to have a look at CoreData which supports SQLite and XML as persistent stores.
XML-Parsing is resource intensive especially if the XML gets very large. CoreData might be able to help you navigate around the hardware limitations of the iOS platform.
精彩评论