cocoa read xml and save xml
使用Cocoa保存XML格式记录文件是本文要介绍的内容,在Cocoa中保存XML的属性列表文件(plist)是很容易的事情。NSArray,NSDictionary,NSString,或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。 下面是保存的方法:
保存下来的结果看起来是这样的:
要想把保存的列表文件读取出来也很简单:
My test
Save:
//test OK! -(void) writePrefs_test { id m_clientName; // outlets to text boxes in the preferences id m_serverName; // window.
NSMutableDictionary * prefs; NSString *aa=@"1243";
m_clientName = aa.copy;
NSString *aa_value; aa_value = m_clientName;
// allocate an NSMutableDictionary to hold our preference data prefs = [[NSMutableDictionary alloc] init];
// our preference data is our client name,and buddy list
//Save:
// [prefs setObject:m_clientName forKey:@"Client1"];
NSArray *classdata1 = [[NSArray alloc] initWithObjects:@"124c","name1",@"22",nil]; [prefs setObject:classdata1 forKey:@"ID1"]; NSArray *classdata2 = [[NSArray alloc] initWithObjects:@"124a","name2",@"11",nil]; [prefs setObject:classdata2 forKey:@"ID2"]; NSArray *classdata3 = [[NSArray alloc] initWithObjects:@"1244","name3",nil]; [prefs setObject:classdata3 forKey:@"ID3"];
NSFileManager *manager = [NSFileManager defaultManager]; NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documents = [path objectAtIndex:0]; NSString *filepath = [documents stringByAppendingPathComponent:@"a.plist"]; if([manager createFileAtPath:filepath contents:nil attributes:nil]) { printf("文件创建成功"); } else { printf("创建失败"); } // save our buddy list to the user's home directory/Library/Preferences. BOOL bSaved; bSaved =[prefs writeToFile:[filepath stringByExpandingTildeInPath] atomically: TRUE];
// BOOL bSaved; // bSaved = [prefs writeToFile:[filepath // stringByExpandingTildeInPath] atomically: YES]; if(TRUE == bSaved) { printf("Save ok! %d",bSaved); } else { printf("Save Failed! %d",bSaved); } // return self; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |