Created
January 2, 2011 03:02
-
-
Save damiandawber/762236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DDXMLParser.m | |
// | |
#import "DDXMLParser.h" | |
@implementation DDXMLParser | |
@synthesize arrayOfDictionaries, delegate; | |
#pragma mark - | |
#pragma mark Initialisation | |
- (id)initWithData:(NSData *)data interestingKeys:(NSSet *)interestingKeys_ | |
itemElement:(NSString *)itemElement_ delegate:(id <DDXMLParserDelegate>) delegate_ { | |
if((self = [super init])) { | |
self.delegate = delegate_; | |
//Initialising vars for use across class | |
interestingKeys = interestingKeys_; | |
itemElement = itemElement_; | |
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; | |
parser.delegate = self; | |
[parser parse]; | |
[parser release]; | |
} | |
return self; | |
} | |
#pragma mark - | |
#pragma mark NSXMLParser delegate methods | |
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { | |
NSLog(@"error: %@", [parseError localizedDescription]); | |
} | |
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validError { | |
NSLog(@"error: %@", [validError localizedDescription]); | |
} | |
- (void) parserDidStartDocument:(NSXMLParser *)parser { | |
self.arrayOfDictionaries = [[NSMutableArray alloc] init]; | |
} | |
- (void)parser: (NSXMLParser *)parser didStartElement: (NSString *) elementName | |
namespaceURI: (NSString *) namespaceURI | |
qualifiedName: (NSString *) qualifiedName | |
attributes: (NSDictionary *) attributeDictionary { | |
//Create a dictionary to store key-object pairs for items in itemElement | |
if([elementName isEqualToString:itemElement]) { | |
tempDictionary = [[NSMutableDictionary alloc] init]; | |
} | |
//If the current item is one specified in interestingKeys, | |
//create a temp string to store it and remember which element it is | |
if ([interestingKeys containsObject:elementName]) { | |
//We prevent duplicate entries for elements of the same name that are | |
//more deeply nested in the XML document - assumes we always want | |
//the first element encountered of name elementName! | |
if([tempDictionary objectForKey:elementName] == nil) { | |
keyInProgress = [elementName copy]; | |
tempString = [[NSMutableString alloc] init]; | |
} | |
} | |
} | |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName | |
namespaceURI:(NSString *)namespaceURI | |
qualifiedName:(NSString *)qName { | |
//If we have come to the end of itemElement, we're done for that item | |
//so add it to the array and release the temp dictionary | |
if([elementName isEqualToString:itemElement]) { | |
[self.arrayOfDictionaries addObject:tempDictionary]; | |
[tempDictionary release]; | |
tempDictionary = nil; | |
} else if ([elementName isEqualToString:keyInProgress]) { | |
//Item is one of our interesting keys so add its contents to the dictionary | |
[tempDictionary setObject:tempString forKey:elementName]; | |
//Release all those vars associated with this element since we're done with it | |
[keyInProgress release]; | |
keyInProgress = nil; | |
[tempString release]; | |
tempString = nil; | |
} | |
} | |
- (void) parser: (NSXMLParser *)parser foundCharacters:(NSString *)string { | |
//Append chars to the tempString if we're at an element in interestingKeys | |
if(tempString != nil) { | |
[tempString appendString:string]; | |
} | |
} | |
- (void) parserDidEndDocument:(NSXMLParser *)parser { | |
NSLog(@"parser did end doc called"); | |
[interestingKeys release]; | |
interestingKeys = nil; | |
[itemElement release]; | |
itemElement = nil; | |
if(self.delegate == nil) { | |
NSLog(@"it's nil!"); | |
} | |
[self.delegate parserDidFinishParsingData:self.arrayOfDictionaries]; | |
} | |
- (void)dealloc { | |
self.arrayOfDictionaries = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment