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. 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. }
  23. @H_301_81@

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

    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>
    20. @H_301_81@

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

      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];
      32. @H_301_81@
        My test @H_823_403@
        Save: @H_823_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_823_403@

猜你在找的XML相关文章