NSMutable Array 到 XML 文件 [英] NSMutable Array to XML file

查看:56
本文介绍了NSMutable Array 到 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Clip 的自定义对象的 NSMutableArray.每个剪辑对象都有一些 NSString 属性(剪辑名称、tcIn、tcOut、文件路径等..).如何在磁盘上写入 xml 文件,其中数组中的每个剪辑都是 xml 文件的一个节点,其属性是该节点的元素?

I have an NSMutableArray of custom objects called Clip. Each clip Object has some NSString properties (clipName,tcIn, tcOut, file path and so on..). How do I write an xml File on disk where each clip in the array is a node of the xml file and its properties are elements of the node?

提前致谢达里奥

推荐答案

我认为这是你应该做的:

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];

这篇关于NSMutable Array 到 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆