亲爱的学者们 我正在尝试使用以下项元素结构解析RSS提要
<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>
请注意与我的问题相关的标签 media:thumbnail.url 。
解析器代码:
// 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;
}
}
}
这似乎是我不熟悉的语法问题,它抓住了存储在media:thumbnail.url中的值。有人可以帮助我这样做吗?
答案 0 :(得分:4)
由于url
是media:thumbnail
元素的属性,您需要通过attributes
中的didStartElement:
字典访问它。 foundCharacters:
消息(我假设您将文本累积到workingPropertyString
中)仅发送给元素的开始和结束标记之间的字符数据。
答案 1 :(得分:1)
Hii Friends,
i have similiar issue of retriving image url from MediaThumbnail attr from my feed,
你可以在didStartElement上写这个,因为它检查媒体:在url里面的thumnail我们可以获取图片url字符串,它适用于我
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];
答案 2 :(得分:0)
轻松获取xml文件中的媒体:缩略图(或任何元素值),
使用 xpath查询。为了执行XPathQuery,您可以使用 libxml2
您的XPath查询听起来像这样://media:thumbnail/@url
并会给你:http://i1.ytimg.com/vi/XBE4AX0Iuvw/default.jpg
读到这个: Using libxml2 for XML parsing and XPath queries in Cocoa(按照说明进行操作)