Thursday, March 24, 2011

Handling RSS Tags with NSXMLParser for iPhone

I've found the following code for parsing through RSS but it does not seem to allow for nested elements:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
    NSLog(@"ended element: %@", elementName);
    if ([elementName isEqualToString:@"item"]) {
     // save values to an item, then store that item into the array...
     [item setObject:currentTitle forKey:@"title"];
     [item setObject:currentLink forKey:@"link"];
     [item setObject:currentSummary forKey:@"summary"];
     [item setObject:currentDate forKey:@"date"];
     [item setObject:currentImage forKey:@"media:thumbnail"];

The RSS to used is:

    <item><title>Knife robberies and burglaries up</title>
<description>The number of robberies carried out at knife-point has increased sharply and burglaries are also up, latest crime figures indicate</description>
<link>http://news.bbc.co.uk/go/rss/-/1/hi/uk/7844455.stm</link>
<guid isPermaLink="false">http://news.bbc.co.uk/1/hi/uk/7844455.stm</guid>
<pubDate>Thu, 22 Jan 2009 13:02:03 GMT</pubDate><category>UK</category>
<media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45400000/jpg/_45400861_policegeneric_pa.jpg"/>
</item>

I need to extract the "url" element from the "media" tag.

Thanks Martin

From stackoverflow
  • You need to get the attributes (including the URL) when the element starts:

    parser:didStartElement:namespaceURI:qualifiedName:attributes:
    
  • As August pointed out, you will have to use didStartElement to get to the attributes of a tag. The attributes are returned as a dictionary with the attribute names as the keys and attribute values as the values.

  • August Coud you Show some example code to store url ? thanks

  • I've just released an open source RSS/Atom Parser for iPhone and hopefully it might be of some use.

    I'd love to hear your thoughts on it too!

  • Hi! Im having the same problem: parsing the image... Can anybody help? Thanks!

0 comments:

Post a Comment