amj amj - 3 months ago 19
iOS Question

XML parsering enclosure

i have two of "enclosure" with deferent "type"...
how can i make the parser "audio/mpeg" parsing the URL OF .mp3 to play it with the MediaPlayer

<enclosure url="http://www.radiozerosix.fr/upload/podcasts/audios/5784338b042d41.79777284.mp3" type="audio/mpeg" length="5986913"/>
<enclosure url="http://www.radiozerosix.fr/upload/podcasts/photos/main/5784338b047468.88036252_mini.png" type="image/png" length="131302"/>

Answer

This is actually pretty easy. You should be able to get the URL from each enclosure element using the attributes dictionary in NSXMLParser's parser:didStartElement:namespaceURI:qualifiedName:attributes: method.

You could retrieve it like this:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
   if ([elementName isEqualToString:@"enclosure"]) {
       [self.mediaURLArray addObject:attributeDict[@"url"];
   }
}

If you only need the audio file's URL, you could also filter for that:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
   if ([elementName isEqualToString:@"enclosure"]) {
        if ([attributeDict[@"type"] isEqualToString:@"audio/mpeg"]) {
                self.mediaURL = attributeDict[@"url"];
        }
    }
}

After doing that, you should be able to stream that audio file using your mediaURL property.

Comments