我正在测试我正在使用这样的RSS源构建新闻Feed iOS应用:http://www.20minutos.es/iphoneapp/feeds/home/
我差不多完成了,但我找不到缩略图的链接。我正在进行这样的解析,我可以找到一些机箱,enclosure2x,缩略图,thumbnail2x,但它们都是空字符串:
- (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);
}
这是我第一次解析rss feed,所以我真的不知道该在那里寻找什么。
由于
答案 0 :(得分:1)
您应该解析media:thumbnail
委托方法中的media:thumbnail2x
和parser:didStartElement:namespaceURI:qualifiedName:attributes:
节点,并从attributeDict
字典中提取缩略图网址:
- (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"];
}
}
如果需要,您可以对机箱执行相同操作。