iPhone RSS Parser

I've made a simple parser class for reading RSS feeds and a simple demo application for the iPhone that implements the iPhoneRSS class.

The following example shows the usage for the RSS class. Basically, the class creates an NSArray of news items, and each object in the array is an NSDictionary. The dictionary contains the element name as the key.

/* load feed from URL */
iPhoneRSS *rss = [[iPhoneRSS alloc] initWithURL:[NSURL URLWithString:@"http://www.reddit.com/.rss"]];
    
/* array contains all posts in feed */
items = [rss items];
    
NSLog(@"Number of items: %d", [items count]);
    
/* loop through all posts in the feed */
NSEnumerator *enumerator = [items objectEnumerator];
NSDictionary *dict;
    
while(dict = [enumerator nextObject]) {
    NSLog(@"---------------------------------------------------------");
    NSLog(@"Title: %@", [dict objectForKey:@"title"]);
    NSLog(@"Description: %@", [dict objectForKey:@"description"]);
    
    /* to get comments, use the rss reader to read the comment RSS for this post, contained in wfw:commentRss */
    NSString *commentURL = [dict objectForKey:@"wfw:commentRss"];
        
    if(commentURL != nil) {
        iPhoneRSS *commentRss = [[iPhoneRSS alloc] initWithURL:[NSURL URLWithString:[dict objectForKey:@"wfw:commentRss"]]];
        NSLog(@"Number of comments: %d", [[commentRss items] count]);
    }        
    NSLog(@"---------------------------------------------------------");
}

You can download the sample project and the RSS class files here: http://www.tech9computers.com/iPhoneRSS.zip

Written on July 17, 2008