首先让自己成为一个游戏类。我们将把 XML 解析为 Games 对象。
Game.h 像这样:
@interface Game : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *gameID;
@property (nonatomic, retain) NSString *gameDescription;
@end
现在在你解析 XML 的类中(在这个例子中是 ViewController),创建一个 NSMutableArray 属性来存储我们解析的 Game 对象,一个 Game 属性,用于我们创建新的 Game 对象,一个 NSString 属性来存储当前我们在 XML 中解析的元素,以及我们正在使用的 NSXMLParser 实例的属性。还要确保它符合 NSXMLParserDelegate 协议。
所以头文件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
现在在实现 ViewController.m 中,我们解析 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);
}
}
解析完成后,我们在 parserDidEndDocument 中得到一个回调:此时 _games 属性将填充我们的 Games 实例。