I'm doing a test where I'm building a news feed iOS app, using rss feeds like this one: http://www.20minutos.es/iphoneapp/feeds/home/
I'm almost done, but I can't find the link to the thumbnail. I'm doing the parsing like this and I can find some enclosure, enclosure2x, thumbnail, thumbnail2x, but they're all empty strings:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
image = [[NSMutableString alloc] init];
image2x = [[NSMutableString alloc] init];
comments = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:link forKey:@"link"];
[item setObject:image forKey:@"image"];
[item setObject:image2x forKey:@"image2x"];
[item setObject:comments forKey:@"comments"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"link"]) {
[link appendString:string];
} else if ([element isEqualToString:@"veinteminutos:numComments"]) {
[comments appendString:[string stringByReplacingOccurrencesOfString:@" " withString:@""]];
}
//NSLog(@"string: %@ \nelement: %@ \n\n\n", string, element);
}
It's my first time parsing rss feeds, so I don't really know what to look for in there.
Thanks
You should parse out the media:thumbnail
and media:thumbnail2x
nodes in the parser:didStartElement:namespaceURI:qualifiedName:attributes:
delegate method and extract the thumbnail URLs from the attributeDict
dictionary:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
image = [[NSMutableString alloc] init];
image2x = [[NSMutableString alloc] init];
comments = [[NSMutableString alloc] init];
} else if ([element isEqualToString:@"media:thumbnail"]) {
image = [attributeDict objectForKey:@"url"];
} else if ([element isEqualToString:@"media:thumbnail2x"]) {
image2x = [attributeDict objectForKey:@"url"];
}
}
You can do the same for the enclosures, should you need them.