Accessing specific child elements when parsing RSS with NSXMLParser
Dear Scholars I am trying to parse a RSS feed with the following item element structure
<item>
<title>Title</title>
<link>http://somelink.com</link>
<description>The description</description>
<author>rss@youtube.com</author>
<guid isPermaLink="false">http://youtube.com/?v=XBE4AX0Iuvw</guid>
<pubDate>Thu, 11 Nov 2010 10:56:44 +0000</pubDate>
<media:title>Media Title</media:title>
<media:thumbnail width="120" url="http://i1.ytimg.com/vi/XBE4AX0Iuvw/default.jpg" height="90"/>
<media:category label="Tags"/>
<media:player url="http://youtube.com/?v=XBE4AX0Iuvw"/>
<media:credit>Credit name</media:credit>
<enclosure url="http://youtube.com/v/XBE4AX0Iuvw.swf" duration="104" type="application/x-shockwave-flash"/>
</item>
Please note the tag media:thumbnail.url of which my question is related to.
Parser code:
// string contants found in the RSS feed
static NSString *kIDStr = @"link";
static NSString *kNameStr = @"thumbnail";
static NSString *kImageStr = @"media:thumbnail";
static NSString *kArtistStr = @"description";
static NSString *kEntryStr = @"item";
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
// entry: { id (link), im:name (app name), im:image (variable height) }
//
NSLog(@"%@",elementName);
// I can see all top level elements,even media:thumbnail
if ([elementName isEqualToString:kEntryStr])
{
self.workingEntry = [[[AppRecord alloc] init] autorelease];
}
storingCharacterData = [elementsToParse containsObject:elementName];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if (self.workingEntry)
{
if (storingCharacterData)
{
NSString *trimmedString = [workingPropertyString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[workingPropertyString setString:@""]; // clear the string for next time
if ([elementName isEqualToString:kIDStr])
{
self.workingEntry.appURLString = trimmedString;
}
else if ([elementName isEqualToString:kNameStr])
{
self.workingEntry.appName = trimmedString;
}
else if ([elementName isEqualToString:kImageStr])
{
NSLog(@"The Content: %@",workingPropertyString); // Comes back empty, I guess due to the fact it has children inside.
// QUESTION: How do I get the value stored in media:thumbnail .url
//NSLog(@">>>>>>>>>>>>> %@ / %@",[elementsToParse objectAtIndex:2],workingPropertyString);
self.workingEntry.imageURLString = trimmedString;
}
else if ([elementName isEqualToString:kArtistStr])
{
self.workingEntry.artist = trimmedString;
}
}
else if ([elementName isEqualToString:kEntryStr])
{
[self.workingArray addObject:self.workingEntry];
self.workingEntry = nil;
}
}
}
It seems to be a matter of syntax which I am not familiar with, which grabs the value stored in media:thumbnail.url. Can someone be as kind as to ass开发者_如何学Goist me in how do do so?
As url
is an attribute of the media:thumbnail
element, you need to access it via the attributes
dictionary in didStartElement:
. The foundCharacters:
message (which I assume is where you're accumulating text into workingPropertyString
) is only sent for character data between an element's start and end tags.
Hii Friends,
i have similiar issue of retriving image url from MediaThumbnail attr from my feed,
you can write this at didStartElement as it check media:thumnail inside url where we can fetch image url String ,and it works for me
if ([elementName isEqualToString:kEntryStr])
{
self.workingEntry = [[[AppRecord alloc] init] autorelease];
}
else if([elementName isEqualToString:@"media:thumbnail"]) {
NSMutableString *currentImgUrl = [[NSMutableString alloc] init];
[currentImgUrl appendString:[attributeDict objectForKey:@"url"]];
NSLog(@"url:%@",currentImgUrl);
self.workingEntry.imageURLString = currentImgUrl;
}
storingCharacterData = [elementsToParse containsObject:elementName];
To get a media:thumbnail (or and any element value) in a xml file easily,
use xpath query. In order to do XPathQuery you can use libxml2
Your XPath Query will sound like that : //media:thumbnail/@url
and will give you : http://i1.ytimg.com/vi/XBE4AX0Iuvw/default.jpg
read this : Using libxml2 for XML parsing and XPath queries in Cocoa (folow the instructions)
精彩评论