加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocoa read xml and save xml

发布时间:2020-12-16 09:39:01 所属栏目:百科 来源:网络整理
导读:使用 Cocoa 保存 XML 格式记录 文件 是本文要介绍的内容,在 Cocoa 中保存 XML 的属性列表 文件 (plist)是很容易的事情。NSArray,NSDictionary,NSString,或者 NSData都可以保存为 XML 格式的plist 文件 。如果NSArray或者NSDictionary中还包含其他可以保

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

下面是保存的方法:

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

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

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

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

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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读