且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSMutable Array 到 XML 文件

更新时间:2022-11-17 10:51:24

我认为这是你应该做的:

I think this is what should do you:

//Create xml string
NSMutableString *xmlString = [[NSMutableString alloc] init];

//Add all the data to the string
[xmlString appendFormat:@"<clips>"];
for (Clip *c in clipsArray)
{ 
    [xmlString appendFormat:@"\n\t<clip>"];
    [xmlString appendFormat:@"\n\t\t<clipName>%@</clipName>", c.clipName];
    [xmlString appendFormat:@"\n\t\t<tcIn>%@</tcIn>", c.tcIn];
    ...
    [xmlString appendFormat:@"</clip>"];
}
[xmlString appendFormat:@"</clips>"];

//Create a variable to represent the filepath of the outputted file
//This is taken from some code which saves to an iPhone app's documents directory, it might not be ideal
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"output.xml"];

//Actually write the information
NSData *data = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil];