0

您好,我正在解析 RSS 和 Atom 提要,我的问题是如何检查 <description> 中的 <img> 和 <url> 标签?

必须有某种检查。谢谢。

这是我解析它们的方法:

- (NSArray *)parseFeed:(NSURL *)feedURL{
    NSError *error;
    NSData *data = [NSData dataWithContentsOfURL:feedURL];
    GDataXMLDocument *xmlParse = [[GDataXMLDocument alloc] initWithData:data error:&error];
    GDataXMLElement *rootElement = xmlParse.rootElement;
    NSArray *array = [[NSArray alloc] init];
    if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
        array = [self parseRSSFeed:rootElement];
        return array;
    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {
        array = [self parseAtomFeed:rootElement];
        return array;
    } else {
        NSLog(@"Unsupported root element: %@", rootElement.name);
        return nil;
    }
}

-(NSArray *)parseRSSFeed:(GDataXMLElement *) rootElement
{
    NSMutableArray *entries = [[NSMutableArray alloc] init];
    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {
        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {
            FeedItem *itemF = [[FeedItem alloc] init];
            itemF.title = [item valueForChild:@"title"];
            itemF.description = [item valueForChild:@"description"];
            NSLog(@"IMAGE - %@", [item valueForChild:@"img"]);
            itemF.dateString = [item valueForChild:@"pubDate"];
            itemF.link = [NSURL URLWithString:[item valueForChild:@"link"]];
            itemF.dateString = [item valueForChild:@"updated"];
            itemF.author = [item valueForChild:@"author"];
            [entries addObject:itemF];
            NSLog(@"RSS - %@", itemF.title);
        }
    }
    NSArray *RSSArray = [entries copy];
    return RSSArray;
}
-(NSArray *)parseAtomFeed:(GDataXMLElement *) rootElement
{
    NSMutableArray *entries = [[NSMutableArray alloc] init];
    NSArray *entry = [rootElement elementsForName:@"entry"];
    for (GDataXMLElement *entryElement in entry) {
        // NSArray *items = [channel elementsForName:@"item"];
        //for (GDataXMLElement *item in items) {
        FeedItem *itemF = [[FeedItem alloc] init];
        itemF.title = [entryElement valueForChild:@"title"];
        itemF.description = [entryElement valueForChild:@"summary"];
        NSArray *links = [entryElement elementsForName:@"link"];
        for (GDataXMLElement *link in links) {
            itemF.link = [NSURL URLWithString:[[link attributeForName:@"href"] stringValue]];
        }
        itemF.dateString = [entryElement valueForChild:@"updated"];
        NSArray *authors = [entryElement elementsForName:@"author"];
        for (GDataXMLElement *authorElement in authors) {
            itemF.author = [authorElement valueForChild:@"name"];
        }
        [entries addObject:itemF];
        NSLog(@"Atom - %@", itemF.title);
    }
    NSArray *atomArray = [entries copy];
    return atomArray;
}

我正在使用 GDataXMLParser 和我自己的解析器类来解析它们。

4

2 回答 2

1

像这样做:

NSString *Str=[item valueForChild:@"description"];
NSArray *tmp=[Str componentsSeparatedByString:@"<Img>"];
if([tmp count]>1){
    NSString *urlstr=[[tmp objectatindex:1] stringByReplacingOccurrencesOfString:@" </Img>" withString:@""];
}

现在urlstr包含您的图片网址。

享受

于 2012-08-08T09:18:08.660 回答
0

我以为

itemF.description = [item valueForChild:@"description"];

NSString。所以你可以使用componentsSeparatedByString“你的标签”。它将返回数组。在位置 1 的数组中,您将获得您的 img 网址。这个网址有右括号“你的结束标签”。用空格替换“你的结束标签”。您将获得图片网址。

希望对你有帮助。

于 2012-08-06T12:50:21.013 回答