RssParser that works


/ Published in: Objective C
Save to your folder(s)

I'm using this Rss Parser for clubplanet app, it works really fast and easy to implement


Copy this code and paste it in your HTML
  1. //
  2. // RssParser.h
  3. // RSS
  4. //
  5. // Copyright 2008 WillowTree Consulting Group, Inc. All rights reserved.
  6. //
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10.  
  11. @interface RssParser : NSObject {
  12. @private
  13. NSMutableDictionary *itemsDictionary;
  14. NSMutableData *webData;
  15.  
  16. NSString *requestUrl;
  17. NSString *currentElement;
  18. BOOL success;
  19. BOOL loading;
  20. BOOL parsed;
  21.  
  22. BOOL inItem;
  23.  
  24. NSInteger callId;
  25. id parentDelegate;
  26. SEL onCompleteCallback;
  27. }
  28.  
  29. - (id)init:(NSInteger)identifier;
  30. - (void)parse:(NSString *)url withDelegate:(id)sender onComplete:(SEL)callback;
  31. - (void)parseResponse;
  32.  
  33. - (NSDictionary *)getRoot;
  34. - (NSArray *)getItems;
  35. - (NSInteger)getId;
  36.  
  37. - (BOOL)isSuccessful;
  38. - (BOOL)isLoading;
  39. - (BOOL)isParsed;
  40.  
  41. @end
  42.  
  43.  
  44.  
  45. //
  46. // RssParser.m
  47. // RSS
  48. //
  49. // Copyright 2008 WillowTree Consulting Group, Inc. All rights reserved.
  50. //
  51.  
  52. #import "RssParser.h"
  53.  
  54.  
  55. @implementation RssParser
  56.  
  57. - (id) init:(NSInteger)identifier {
  58. self = [super init];
  59.  
  60. if (self) {
  61. callId = identifier;
  62. root = [[NSMutableDictionary alloc] init];
  63. items = [[NSMutableArray alloc] init];
  64. success = NO;
  65. loading = NO;
  66. parsed = NO;
  67. currentElement = nil;
  68. }
  69.  
  70. return self;
  71. }
  72.  
  73. - (void)parse:(NSString *)url withDelegate:(id)sender onComplete:(SEL)callback {
  74. parentDelegate = sender;
  75. onCompleteCallback = callback;
  76. requestUrl = [url retain];
  77. loading = YES;
  78.  
  79. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  80.  
  81. [request setURL:[[NSURL alloc] initWithString:requestUrl]];
  82. [request setHTTPMethod:@"GET"];
  83. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  84.  
  85. [[NSURLConnection alloc] initWithRequest:request delegate:self];
  86. [request release];
  87.  
  88. webData = [[NSMutableData data] retain];
  89. }
  90.  
  91. - (BOOL)isSuccessful {
  92. return success;
  93. }
  94.  
  95. - (BOOL)isLoading {
  96. return loading;
  97. }
  98.  
  99. - (BOOL)isParsed {
  100. return parsed;
  101. }
  102.  
  103. - (NSInteger)getId {
  104. return callId;
  105. }
  106.  
  107. - (NSArray *)getItems {
  108. return items;
  109. }
  110.  
  111. - (NSDictionary *)getRoot {
  112. return root;
  113. }
  114.  
  115. // HTTP Request Handling functionality
  116. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  117. success = NO;
  118. loading = NO;
  119.  
  120. if ([parentDelegate respondsToSelector:onCompleteCallback]) {
  121. [parentDelegate performSelector:onCompleteCallback withObject:self];
  122. }
  123. }
  124.  
  125. - (void)connection:(NSURLConnection *)connection didReceivedAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  126. success = NO;
  127. }
  128.  
  129. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  130. [webData appendData:data];
  131. }
  132.  
  133. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  134. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  135. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  136.  
  137. NSLog(@"RssParser: Response is an NSHTTPURLResponse: Response=%d", [httpResponse statusCode]);
  138.  
  139. // Does not handle authentication quite yet.
  140. if ([httpResponse statusCode] >= 400 && [httpResponse statusCode] <= 599) {
  141. success = NO;
  142. } else if ([httpResponse statusCode] >= 100 && [httpResponse statusCode] <= 299) {
  143. success = YES;
  144. } else {
  145. NSLog(@"RssParser: Status code is unknown.");
  146. }
  147. }
  148. }
  149.  
  150. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  151. NSString *dataString = [[[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding] autorelease];
  152.  
  153. if ([dataString length] > 0) {
  154. [self parseResponse];
  155. }
  156.  
  157. loading = NO;
  158. }
  159.  
  160. - (void)parseResponse {
  161. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:webData];
  162.  
  163. [parser setDelegate:self];
  164. [parser setShouldProcessNamespaces:NO];
  165. [parser setShouldReportNamespacePrefixes:NO];
  166. [parser setShouldResolveExternalEntities:NO];
  167.  
  168. [parser parse];
  169. }
  170.  
  171. // XML Parser functionality
  172.  
  173. - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
  174. NSLog(@"RestResponse: Parsing error occurred.");
  175. parsed = NO;
  176. loading = NO;
  177. }
  178.  
  179. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
  180. qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  181. NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  182.  
  183. currentElement = element;
  184.  
  185. if ([[currentElement lowercaseString] isEqual:@"item"]) {
  186. inItem = YES;
  187. itemsDictionary = [[NSMutableDictionary alloc] init];
  188. }
  189. }
  190.  
  191. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
  192. qualifiedName:(NSString *)qName {
  193. NSString *element = [elementName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  194.  
  195. if ([[element lowercaseString] isEqual:@"item"]) {
  196. inItem = NO;
  197. [items addObject:itemsDictionary];
  198. }
  199. }
  200.  
  201. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  202. NSString *stringValue = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  203. NSString *element = [currentElement stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  204.  
  205. // Skip over blank elements.
  206. if (stringValue == nil || [stringValue isEqual:@""]) {
  207. return;
  208. }
  209.  
  210. if (element != nil && [element length] > 0) {
  211. if (inItem) {
  212. if ([itemsDictionary objectForKey:element] != nil) {
  213. // If we're adding categories, we can safely add a comma. Otherwise, we don't, and append the string data.
  214. if ([element isEqual:@"category"]) {
  215. [itemsDictionary setObject:[NSString stringWithFormat:@"%@, %@", [itemsDictionary objectForKey:element], stringValue]
  216. forKey:element];
  217. } else {
  218. [itemsDictionary setObject:[NSString stringWithFormat:@"%@%@", [itemsDictionary objectForKey:element], stringValue]
  219. forKey:element];
  220. }
  221. } else {
  222. [itemsDictionary setObject:stringValue forKey:element];
  223. }
  224. } else {
  225. if ([root objectForKey:element] != nil) {
  226. if ([element isEqual:@"category"]) {
  227. [root setObject:[NSString stringWithFormat:@"%@, %@", [root objectForKey:element], stringValue] forKey:element];
  228. } else {
  229. [root setObject:[NSString stringWithFormat:@"%@%@", [root objectForKey:element], stringValue] forKey:element];
  230. }
  231. } else {
  232. [root setObject:stringValue forKey:element];
  233. }
  234. }
  235. }
  236. }
  237.  
  238. - (void)parserDidStartDocument:(NSXMLParser *)parser {
  239. NSLog(@"RssParser: Started document.");
  240. }
  241.  
  242. - (void)parserDidEndDocument:(NSXMLParser *)parser {
  243. parsed = YES;
  244. loading = NO;
  245.  
  246.  
  247. if ([parentDelegate respondsToSelector:onCompleteCallback]) {
  248. [parentDelegate performSelector:onCompleteCallback withObject:self];
  249. }
  250. }
  251.  
  252.  
  253. - (void)dealloc {
  254. [super dealloc];
  255.  
  256. [requestUrl release];
  257. requestUrl = nil;
  258.  
  259. [webData release];
  260. webData = nil;
  261. }
  262.  
  263. @end

URL: http://code.google.com/p/iphone-simple-rss-aggregator/source/browse/trunk/Simple+RSS/Classes/RssParser.m

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.