我正在尝试解析rss Feed。我可以解析BBC的feed。但是当我尝试解析独立的源时,我无法捕捉到我需要的图像的链接。 (Feed的来源是:http://www.independent.co.uk/sport/football/premier-league/rss) 这是饲料的摘录。它的1项。
<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>
问题是提取'img src =',因为我想要显示缩略图的网址。
我正在使用NSXMLParser解析rss feed并在后面的委托中进行解析。
- (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:@"title"])
{
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];
}
}
我已经退出了
else if([elementName isEqualToString:@"description"])
{
self.currentItem.description = self.currentItemValue;
}
self.currentItemValue的内容,它不包含任何img url文本。
我错过了什么?它是不是有效的XML,所以使用NSXMLParser永远不会对我有用吗?
非常感谢, -code
答案 0 :(得分:0)
我建议在NSXMLParser周围使用XML包装器。
我使用的是TouchXML,但还有其他几个。
给定一个xPath(在你的情况下是//项目);包装器将为您提供NSDictionary
,因此您可以使用valueForKey:
的{{1}}方法检索值。
比为特定类创建自定义解析器更容易和可重用。