0

我正在做一个测试,我正在构建一个新闻提要 iOS 应用程序,使用像这样的 rss 提要:http ://www.20minutos.es/iphoneapp/feeds/home/

我快完成了,但我找不到缩略图的链接。我正在做这样的解析,我可以找到一些附件,enclosure2x,thumbnail,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 提要,所以我真的不知道在那里寻找什么。

谢谢

4

1 回答 1

1

您应该解析出委托方法中的media:thumbnailmedia:thumbnail2x节点,并从字典中parser:didStartElement:namespaceURI:qualifiedName:attributes:提取缩略图 URL :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"];
    }
}

如果需要,您可以对外壳执行相同操作。

于 2013-10-24T11:19:15.247 回答