I'm trying to parse an rss feed. I can parse the BBC's feed ok. But when I try and parse the independant's feed I cannot catch the link to an image I need. (Source of feed is: http://www.independent.co.uk/sport/football/premier-league/rss) Here is an extract from the feed. Its 1 item.
<item>
<title>Juventus join race to sign Carlos Tevez claim Corinthians</title>
<guid>http://www.independent.co.uk/sport/football/transfers/juventus-join-race-to-sign-
carlos-tevez-claim-corinthians-2314261.html</guid>
<link>http://www.independent.co.uk/sport/football/transfers/juventus-join-race-to-sign-
carlos-tevez-claim-corinthians-2314261.html</link>
<description>
<img src="http://www.independent.co.uk/multimedia/dynamic/00623/tevez_623081k.jpg"
style="padding-right:5px;margin-right:5px" align="left" >
<p>
Corinthians president Andres Sanchez claims Juventus have entered the race to
sign Carlos Tevez but insists his own club will not increase their offer for
the want-away Argentinian.
</p> </description>
<category>Transfers</category>
<pubDate>Fri, 15 Jul 2011 10:46:12 +0100</pubDate>
</item>
The problem is extracting 'img src=' since I want the url to the thumbnail image to display.
I'm parsing the rss feed with NSXMLParser and doing the parsing in there following delegates.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if(nil != qualifiedName)
{
elementName = qualifiedName;
}
if ([elementName isEqualToString:@"item"])
{
self.currentItem = [[[BlogRss alloc]init]autorelease];
}
else if ([elementName isEqualToString:@"media:thumbnail"])
{
self.currentItem.mediaUrl = [attributeDict valueForKey:@"url"];
}
else if([elementName isEqualToString:@"title"] || [elementName isEqualToString:@"link"] || [elementName isEqualToString:@"guid"] || [elementName isEqualToString:@"pubDate"])
{
self.currentItemValue = [NSMutableString string];
}
else if ([elementName isEqualToString:@"description"] )
{
self.currentItemValue = [NSMutableString string];
}
else
{
self.currentItemValue = nil;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(nil != qName)
{
elementName = qName;
}
if([elementName isEqualToString:@"ti开发者_如何学编程tle"])
{
self.currentItem.title = self.currentItemValue;
}
else if([elementName isEqualToString:@"description"])
{
self.currentItem.description = self.currentItemValue;
}
else if([elementName isEqualToString:@"link"])
{
self.currentItem.linkUrl = self.currentItemValue;
}
else if([elementName isEqualToString:@"guid"])
{
self.currentItem.guidUrl = self.currentItemValue;
}
else if([elementName isEqualToString:@"pubDate"])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
self.currentItem.pubDate = [NSDate dateFromInternetDateTimeString:self.currentItemValue formatHint:DateFormatHintRFC822];
[formatter release];
}
else if([elementName isEqualToString:@"item"])
{
[[self rssItems] addObject:self.currentItem];
}
}
I've NSLog'd out
else if([elementName isEqualToString:@"description"])
{
self.currentItem.description = self.currentItemValue;
}
The contents of self.currentItemValue and it does not contain any of the img url text.
What is it that I am missing. Is it not valid XML so using NSXMLParser will never work for me?
Many Thanks, -Code
I would recommend using an XML wrapper around NSXMLParser.
The one I have used is TouchXML but there are several others out there.
Given an xPath (//item in your case); the wrappers will provide you with NSDictionary
, so you can retrieve the values using the valueForKey:
method of NSDictionary
.
It is a lot easier and reusable than creating a custom parser for your specific class.
精彩评论