Saving to the XML file data

To store the data in XML, we will first add the value we want to store in the players array we created. Then, we will take this modified array list and call the saveXMLGroup function, which will write the value into the XML document.

How to do it…

First, let's create the addXMLPlayer function, which will add a new player to the array, as follows:

-(void) addXMLPlayer:(Player)player {

  NSValue *value = [NSValuevalueWithBytes:&player objCType:@encode(Player)];

  [playersaddObject:value];

}

There is nothing fancy here; we are just taking the player and storing it in NSValue and then adding the value to the players array, as follows:

- (void) saveXMLGroup{

  GDataXMLElement * groupElement= [GDataXMLNodeelementWithName:@"Group"];

  NSLog(@"Player count: %lu", (unsigned long)players.count);

  for(inti = 0; i<players.count ; i++) {

    Player player;
    NSValue *value = [players objectAtIndex:i];
    [valuegetValue:&player];

    GDataXMLElement * playerElement = [GDataXMLNodeelementWithName:@"Player"];

    GDataXMLElement * nameElement = [GDataXMLNodeelementWithName:@"Name" stringValue:player.name];

    GDataXMLElement * levelElement = [GDataXMLNodeelementWithName:@"Level" stringValue:
      [NSStringstringWithFormat:@"%d", player.level]];

    [playerElementaddChild:nameElement];
    [playerElementaddChild:levelElement];


    [groupElementaddChild:playerElement];
  }

  GDataXMLDocument *document = [[GDataXMLDocumentalloc] initWithRootElement:groupElement];

  NSData *xmlData = document.XMLData;

  NSString *filePath = [self XMLdataFilePath:TRUE];
  NSLog(@"Saving xml data to %@...", filePath);
  [xmlDatawriteToFile:filePathatomically:YES];

}

Here, we will get each of the player properties and add them to playerElement using GDataXMLElementand. Finally, we will take playerElement and add it to groupElement, which is the exact opposite process of what we did earlier while getting the values from the XML file.

How it works…

To verify that it actually works, we will add the following in the init function:

-(id)init{

  if(self = [super init]){


    //** XML

    players = [[NSMutableArrayalloc]init];

    [selfloadXMLGroup];


    Player player;
    player.name = @"Bob";
    player.level = 5;

    [selfaddXMLPlayer:player];
    [selfsaveXMLGroup];
    [selfloadXMLGroup];
  }

  return self;
}

Here, we will create a new Player variable called player and then assign it a name and level. Then, we will pass this player into the addXMLPlayer function to add it to the array. After that, we will call the saveXMLgroup function to save the changes to the XML file. Finally, we will load the file again.

Here is the final result:

How it works…

It should also be noted that the file doesn't get saved in the same location as before inside your project folder. It is stored at a different location.

In this case, it is stored in the following file:

How it works…

Also, to show you that the file actually contains the data, here is a screenshot of the contents and location of the file:

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.119