1

我正在使用附件标签解析 xml。我怎样才能做到这一点?我可以轻松地获取链接、guid、标题和 pubDate 标记并将它们解析为 NSString,但如果我尝试从附件标记创建字符串,它会返回 null。

我只需要在播客剧集的这一部分中找到的 URL:

<enclosure url="http://treymorgan.podbean.com/mf/feed/bf8mvq/Accommoditions.mp3" length="29865594" type="audio/mpeg"/>
4

2 回答 2

1

如果您使用的是 NSXMLParser,您可以像这样读取 url 属性(假设您有一个名为 podcastURL 的属性来存储解析后的 URL):

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
   if ([elementName isEqualToString:@"enclosure"]) {
      self.podcastURL = [attributeDict objectForKey:@"url"];
   }
}
于 2012-07-14T20:02:18.917 回答
0

这是对上述问题的直接答案。我遇到了同样的事情。得益于此ATOM 数据解析与 GData 帖子的排序

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries
{

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {

        NSString *blogTitle = [channel valueForChild:@"title"];

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {
            NSString *podcastEnclosureUrl =  nil;

            NSString *articleTitle = [item valueForChild:@"title"];

            NSArray *enclosureArray = [item elementsForName:@"enclosure"]; //this is the code for what I needed
            for (GDataXMLElement *content in enclosureArray)
            {
                NSString *podcastEnclosureUrl = [[content attributeForName:@"url"] stringValue];
                NSLog(@"URL: %@", podcastEnclosureUrl);
            }
            NSString *articleDateString = [item valueForChild:@"pubDate"];
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];

            RSSPodcastFeed *entry = [[RSSPodcastFeed alloc] initWithBlogTitle:blogTitle
                                                     articleTitle:articleTitle
                                                       podcastEnclosureUrl:podcastEnclosureUrl
                                                      articleDate:articleDate];
            [entries addObject:entry];

        }
    }

}
于 2013-04-23T20:38:11.253 回答