我从Web查询中返回了一些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>
我正在使用NSXMLParser
来解析它,并且当我对它们进行NSLog时,它会逐行将每一行吐出到我的控制台中。我正在尝试将每个<game>
提供给我的Game
个对象中,其名称为NSString,ID为NSNumber等。但是,我正在努力弄清楚我是如何判断的它开始一个新对象,因为在我的任何NSLog语句中都没有返回<game>
标记,只有那些有实际数据的标记(例如每个名称,id等)
如果我想将每个<game> </game>
标记内的所有数据都放到一个单独的对象中,我该怎么办?这是解析器代码:
- (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);
}
答案 0 :(得分:2)
首先让自己成为一个游戏类。我们将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属性。