cocoa read xml and save xml

前端之家收集整理的这篇文章主要介绍了cocoa read xml and save xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用Cocoa保存XML格式记录文件是本文要介绍的内容,在Cocoa中保存XML属性列表文件(plist)是很容易的事情。NSArray,NSDictionary,NSString,或者 NSData都可以保存为XML格式的plist文件。如果NSArray或者NSDictionary中还包含其他可以保存为属性列表的对象,它们可以一起存储在plist文件中。

下面是保存的方法

  1. @interfaceClientDisplayMgr{
  2. @H_404_25@…
  3. IBOutletidm_clientName;//outletstotextBoxesinthepreferences
  4. @H_404_25@IBOutletidm_serverName;//window.
  5. NSArray*m_availableFriends;
  6. @H_404_25@…
  7. }
  8. @H_404_25@@end
  9. @H_404_25@//
  10. //SavesomevarIoUspreferences
  11. @H_404_25@-writePrefs
  12. {
  13. @H_404_25@NSMutableDictionary*prefs;
  14. @H_404_25@//allocateanNSMutableDictionarytoholdourpreferencedata
  15. prefs=[[NSMutableDictionaryalloc]init];
  16. @H_404_25@
  17. //ourpreferencedataisourclientname,hostname,andbuddylist
  18. @H_404_25@[prefssetObject:[m_clientNamestringValue]forKey:@"Client"];
  19. [prefssetObject:[m_serverNamestringValue]forKey:@"Server"];
  20. @H_404_25@[prefssetObject:m_friendsforKey:@"Friends"];
  21. @H_404_25@//saveourbuddylisttotheuser'shomedirectory/Library/Preferences.
  22. [prefswriteToFile:[@"~/Library/Preferences/MiniMessageClient.plist"
  23. @H_404_25@stringByExpandingTildeInPath]atomically:TRUE];
  24. returnself;
  25. @H_404_25@}

保存下来的结果看起来是这样的:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. @H_404_25@<!DOCTYPEplistSYSTEM"file://localhost/System/Library/DTDs/PropertyList.dtd">
  3. <plistversion="0.9">
  4. @H_404_25@<dict>
  5. <key>Client</key>
  6. @H_404_25@<string>CrazyJoe</string>
  7. <key>Friends</key>
  8. @H_404_25@<array>
  9. <string>CrazyJoe</string>
  10. @H_404_25@<string>Jim</string>
  11. <string>Joe</string>
  12. @H_404_25@<string>CrazyJim</string>
  13. <string>Jose</string>
  14. @H_404_25@<string>CrazyJoe</string>
  15. </array>
  16. @H_404_25@<key>Server</key>
  17. <string>localhost</string>
  18. @H_404_25@</dict>
  19. </plist>

要想把保存的列表文件读取出来也很简单:

  1. -awakeFromNib
  2. @H_404_25@{
  3. NSString*clientName,*serverName;
  4. @H_404_25@NSDictionary*prefs;
  5. @H_404_25@//loadthepreferencesdictionary
  6. prefs=[NSDictionarydictionaryWithContentsOfFile:
  7. @H_404_25@[@"~/Library/Preferences/MiniMessageClient.plist"
  8. stringByExpandingTildeInPath]];
  9. @H_404_25@
  10. //ifthefilewasthere,wegotalltheinformationweneed.
  11. @H_404_25@//(notethatit'sprobablyagoodideatoindividuallyverifyobjects
  12. //wepulloutofthedictionary,butthisisexamplecode
  13. @H_404_25@if(prefs){
  14. //
  15. @H_404_25@//writeourloadednamesintothepreferencedialog'stextBoxes.
  16. [m_clientNamesetStringValue:[prefsobjectForKey:@"Client"]];
  17. @H_404_25@[m_serverNamesetStringValue:[prefsobjectForKey:@"Server"]];
  18. @H_404_25@//
  19. //loadourfriendlist.
  20. @H_404_25@m_friends=[[prefsobjectForKey:@"Friends"]retain];
  21. }else{
  22. @H_404_25@//
  23. //nopropertylist.Thenibfile'sgotdefaultsforthe
  24. @H_404_25@//preferencedialogBox,butwestillneedalistoffriends.
  25. m_friends=[[NSMutableArrayalloc]init];
  26. @H_404_25@//we'reouronlyfriend(isn'titstrangetalkingaboutweinthesingular?)
  27. [m_friendsaddObject:[m_clientNamestringValue]];
  28. @H_404_25@}
  29. @H_404_25@//
  30. //getourpreferencedatafortherestofawakeFromNib
  31. @H_404_25@clientName=[m_clientNamestringValue];
  32. serverName=[m_serverNamestringValue];
  33. @H_404_25@…
My test @H_533_403@
Save: @H_533_403@

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

}

@H_533_403@

猜你在找的XML相关文章