Last active
January 3, 2016 03:49
-
-
Save jchudzynski/8404505 to your computer and use it in GitHub Desktop.
NSXMLParser Example
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
// XMLParser.m | |
// CDT | |
// Created by Janusz Chudzynski on 1/13/14. | |
#import "XMLParser.h" | |
#import "Deck.h" | |
#import "Question.h" | |
typedef void (^SuccessBlock)(NSData *); | |
@interface XMLParser()<NSXMLParserDelegate> | |
@property (nonatomic,strong) NSMutableString * content; | |
@property (nonatomic,strong) NSXMLParser *parser; | |
@property (nonatomic,copy) void (^completionBlock)(id data); | |
@property (nonatomic,copy) void (^errorBlock)(NSError * error); | |
//Data fields go here | |
@property(nonatomic, strong) NSMutableArray * parsedContent; | |
@property(nonatomic, strong) Deck * currentDeck; | |
@property(nonatomic, strong) Question * currentQuestion; | |
@end | |
@implementation XMLParser | |
-(void)parseData:(NSData *)data completionBlock:(void (^)(id data))completionBlock errorBlock: (void (^)(NSError * error))errorBlock | |
{ | |
self.completionBlock = completionBlock; | |
self.errorBlock=errorBlock; | |
self.content = [NSMutableString new]; | |
self.parsedContent = [NSMutableArray new]; | |
self.parser = [[NSXMLParser alloc]initWithData:data]; | |
self.parser.delegate =self; | |
[self.parser parse]; | |
} | |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict; | |
// sent when the parser finds an element start tag. | |
{ | |
//reset content | |
[self.content deleteCharactersInRange:NSMakeRange(0, self.content.length)]; | |
//create an element here | |
if([elementName isEqualToString:@"Deck"]) | |
{ | |
self.currentDeck = [[Deck alloc]init]; | |
} | |
if([elementName isEqualToString:@"Question"]) | |
{ | |
self.currentQuestion = [[Question alloc]init]; | |
} | |
} | |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string; | |
// This returns the string of the characters encountered thus far. You may not necessarily get the longest character run. The parser reserves the right to hand these to the delegate as potentially many calls in a row to -parser:foundCharacters: | |
{ | |
[self.content appendString:string]; | |
} | |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName; | |
// sent when an end tag is encountered. The various parameters are supplied as above. | |
{ | |
if([elementName isEqualToString:@"Question"]) | |
{ | |
[self.currentDeck.questions addObject:self.currentQuestion]; | |
} | |
if([elementName isEqualToString:@"Deck"]) | |
{ | |
// add it to the content | |
[self.parsedContent addObject:self.currentDeck]; | |
} | |
[self.content deleteCharactersInRange:NSMakeRange(0, self.content.length)]; | |
} | |
// sent when the parser has completed parsing. If this is encountered, the parse was successful. | |
- (void)parserDidEndDocument:(NSXMLParser *)parser;{ | |
self.completionBlock(self.parsedContent); | |
} | |
// ...and this reports a fatal error to the delegate. The parser will stop parsing. | |
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;{ | |
self.errorBlock(parseError); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment