/ Published in: Objective C
I'm using this Rss Parser for clubplanet app, it works really fast and easy to implement
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// // RssParser.h // RSS // // Copyright 2008 WillowTree Consulting Group, Inc. All rights reserved. // #import <Foundation/Foundation.h> @private NSMutableDictionary *root; NSMutableDictionary *itemsDictionary; NSMutableArray *items; NSMutableData *webData; NSString *requestUrl; NSString *currentElement; BOOL success; BOOL loading; BOOL parsed; BOOL inItem; NSInteger callId; id parentDelegate; SEL onCompleteCallback; } - (id)init:(NSInteger)identifier; - (void)parseResponse; - (NSInteger)getId; - (BOOL)isSuccessful; - (BOOL)isLoading; - (BOOL)isParsed; @end // // RssParser.m // RSS // // Copyright 2008 WillowTree Consulting Group, Inc. All rights reserved. // #import "RssParser.h" @implementation RssParser - (id) init:(NSInteger)identifier { self = [super init]; if (self) { callId = identifier; success = NO; loading = NO; parsed = NO; currentElement = nil; } return self; } parentDelegate = sender; onCompleteCallback = callback; requestUrl = [url retain]; loading = YES; [request setHTTPMethod:@"GET"]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request release]; } - (BOOL)isSuccessful { return success; } - (BOOL)isLoading { return loading; } - (BOOL)isParsed { return parsed; } - (NSInteger)getId { return callId; } return items; } return root; } // HTTP Request Handling functionality success = NO; loading = NO; if ([parentDelegate respondsToSelector:onCompleteCallback]) { [parentDelegate performSelector:onCompleteCallback withObject:self]; } } - (void)connection:(NSURLConnection *)connection didReceivedAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { success = NO; } [webData appendData:data]; } NSLog(@"RssParser: Response is an NSHTTPURLResponse: Response=%d", [httpResponse statusCode]); // Does not handle authentication quite yet. if ([httpResponse statusCode] >= 400 && [httpResponse statusCode] <= 599) { success = NO; } else if ([httpResponse statusCode] >= 100 && [httpResponse statusCode] <= 299) { success = YES; } else { NSLog(@"RssParser: Status code is unknown."); } } } if ([dataString length] > 0) { [self parseResponse]; } loading = NO; } - (void)parseResponse { [parser setDelegate:self]; [parser setShouldProcessNamespaces:NO]; [parser setShouldReportNamespacePrefixes:NO]; [parser setShouldResolveExternalEntities:NO]; [parser parse]; } // XML Parser functionality NSLog(@"RestResponse: Parsing error occurred."); parsed = NO; loading = NO; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; currentElement = element; if ([[currentElement lowercaseString] isEqual:@"item"]) { inItem = YES; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([[element lowercaseString] isEqual:@"item"]) { inItem = NO; [items addObject:itemsDictionary]; } } NSString *stringValue = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSString *element = [currentElement stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // Skip over blank elements. if (stringValue == nil || [stringValue isEqual:@""]) { return; } if (element != nil && [element length] > 0) { if (inItem) { if ([itemsDictionary objectForKey:element] != nil) { // If we're adding categories, we can safely add a comma. Otherwise, we don't, and append the string data. if ([element isEqual:@"category"]) { [itemsDictionary setObject:[NSString stringWithFormat:@"%@, %@", [itemsDictionary objectForKey:element], stringValue] forKey:element]; } else { [itemsDictionary setObject:[NSString stringWithFormat:@"%@%@", [itemsDictionary objectForKey:element], stringValue] forKey:element]; } } else { [itemsDictionary setObject:stringValue forKey:element]; } } else { if ([root objectForKey:element] != nil) { if ([element isEqual:@"category"]) { [root setObject:[NSString stringWithFormat:@"%@, %@", [root objectForKey:element], stringValue] forKey:element]; } else { [root setObject:[NSString stringWithFormat:@"%@%@", [root objectForKey:element], stringValue] forKey:element]; } } else { [root setObject:stringValue forKey:element]; } } } } NSLog(@"RssParser: Started document."); } parsed = YES; loading = NO; if ([parentDelegate respondsToSelector:onCompleteCallback]) { [parentDelegate performSelector:onCompleteCallback withObject:self]; } } - (void)dealloc { [super dealloc]; [requestUrl release]; requestUrl = nil; [webData release]; webData = nil; } @end