user717452
user717452

Reputation: 111

iPhone Parse XML Enclosure

I am working on parsing an xml with an enclosure tag. How can I do this? I can easily take the link, guid, title, and pubDate tags and parse them into an NSString, but if I try to create a string from enclosure tag, it comes back null.

I just need the URL that is found in this part of the podcast episode:

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

Upvotes: 1

Views: 654

Answers (2)

DenVog
DenVog

Reputation: 4286

Here is a direct answer to the above problem. I was running into the same thing. Sorted thanks to this 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];

        }
    }

}

Upvotes: 0

joern
joern

Reputation: 27620

If you are using NSXMLParser you can read the url attribute like this (providing that you have a property called podcastURL to store the parsed 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"];
   }
}

Upvotes: 1

Related Questions