Reputation: 9700
I have some XML being returned from a web query, with multiple parameters wrapped up inside a tag, like so:
<game>
<name>First game title</name>
<id>12345</id>
<desc>A game..</desc>
</game>
<game>
<name>Second game title</name>
<id>67890</id>
<desc>Another game..</desc>
</game>
I'm using NSXMLParser
to parse it, and it's spitting out each line one by one into my console as I NSLog them. I'm trying to feed each <game>
into one of my Game
objects, with name as an NSString, ID as an NSNumber, etc. However, I'm struggling to work out how I'd tell it to begin a new object, since the <game>
tag isn't being returned in any of my NSLog statements, only those with actual data are (such as each name, id, etc.)
If I want to get all of the data within each <game> </game>
tag into a separate object, how can I do so? Here's the parser code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
element = [NSMutableString string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(@"ELEMENT TYPE: %@ VALUE: %@", elementName, element);
}
Upvotes: 1
Views: 428
Reputation: 492
First make yourself a Game class. We will parse the XML into Games objects.
Game.h like so:
@interface Game : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *gameID;
@property (nonatomic, retain) NSString *gameDescription;
@end
Now in the class you are parsing the XML in (in this example ViewController), create a NSMutableArray property to store the Game objects as we parse them, a Game property to use as we create new Game objects, a NSString property to store the current element we are parsing in the XML, and a property for the NSXMLParser instance we are using. Also make sure it conforms to the NSXMLParserDelegate protocol.
So the header ViewController.h:
@interface ViewController : UIViewController <NSXMLParserDelegate>
@property (nonatomic, retain) NSString *currentElement;
@property (nonatomic, retain) NSMutableArray *games;
@property (nonatomic, retain) Game *gameBeingParsed;
@property (nonatomic, retain) NSXMLParser *xmlParser;
@end
Now in the implementation ViewController.m we parse the XML:
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Make an NSMutableArray to put the parsed Game objects in
self.games = [NSMutableArray array];
// Get the XML data to parse
// We need it in an NSdata object
NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?><xml><game><name>First game title</name><id>12345</id><desc>A game..</desc></game><game><name>Second game title</name><id>67890</id><desc>Another game..</desc></game></xml>";
NSData *xmlData = [xmlString dataUsingEncoding:NSStringEncodingConversionAllowLossy];
// Set up an NSXMLParser to use
// Set the delegate and start parsing!
self.xmlParser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
_xmlParser.delegate = self;
[_xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
// If we have a <game> tag then we are starting to parse a new Game object
if ([elementName isEqualToString:@"game"]) {
self.gameBeingParsed = [[[Game alloc] init] autorelease];
}
// If not then we need to keep track of the element name so we know which property to set on the Game object
else {
self.currentElement = elementName;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// If we have a closing </game> tag we are done parsing a Game so add it to the array
if ([elementName isEqualToString:@"game"]) {
[_games addObject:_gameBeingParsed];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// Work out which element we have the characters for
// Then set the property of the Game object
if ([_currentElement isEqualToString:@"name"]){
_gameBeingParsed.name = string;
}
if ([_currentElement isEqualToString:@"id"]){
_gameBeingParsed.gameID = [NSNumber numberWithInt:[string intValue]];
}
if ([_currentElement isEqualToString:@"name"]){
_gameBeingParsed.gameDescription = string;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
// We are done parsing XML
NSLog(@"Parsed %d Games", _games.count);
for (Game *game in _games) {
NSLog(@"%@ : %@ : %@", game.name, game.gameID, game.gameDescription);
}
}
After parsing has finished and we get a call back in parserDidEndDocument: At this point the _games property will be populated we instances of Games.
Upvotes: 2