iPhone Parse XML Enclosure

时间:2012-07-14 18:10:23

标签: iphone xml xcode nsstring xml-parsing

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

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

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

2 个答案:

答案 0 :(得分: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"];
   }
}

答案 1 :(得分:0)

这是对上述问题的直接回答。我遇到了同样的事情。排序归功于此ATOM data parse with GData post

- (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];

        }
    }

}