我有一个RSS解析项目。这是我的RSS网址;
http://vimeo.com/udhdhmi/videos/rss
我正在使用NSXMLParser方法解析。
- (void)rssParseStart{
NSURL *url = [NSURL URLWithString:@"http://vimeo.com/udhdhmi/videos/rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:@"item"]) {
model = [[VideoModel alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:@"title"]) {
[model.videoTitle appendString:string];
}
if ([element isEqualToString:@"pubDate"]) {
[model.videoPubDate appendString:string];
}
if ([element isEqualToString:@"link"]) {
[model.videoLink appendString:string];
}
if ([element isEqualToString:@"description"]) {
[model.videoDescription appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[feeds addObject:model];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
[self.tableView reloadData];
}
到目前为止没有问题,但是来自“描述”标签的数据是这样的; - 实施例;
<p><a href="http://vimeo.com/107905906"><img src="http://i.vimeocdn.com/video/491445051_200x150.jpg" alt="" /></a></p><p><p class="first"></p></p><p><strong>Cast:</strong> <a href="http://vimeo.com/udhdhmi">DHMI</a></p><p><strong>Tags:</strong> </p>
我只想从该数据中获取图片网址。 谢谢你的兴趣。
答案 0 :(得分:1)
您可以通过修剪从xml解析器获取的字符串值来获取该数据的URL
// it is assumed that 'string' variable holds your current data
NSRange range;
int index;
range = [string rangeOfString:@"img src=\""];
index = range.location + range.length;
NSString* trimmedString = [string substringFromIndex:index];
range = [trimmedString rangeOfString:@"\""];
index = range.location + range.length - 1;
NSString* finalValue = [trimmedString substringToIndex:index];
finalValue将url存储为字符串
答案 1 :(得分:0)
解决方案:
- (void)rssParseStart{
self.progressBar = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.progressBar.labelText = @"Lütfen bekleyiniz...";
NSURL *url = [NSURL URLWithString:@"http://vimeo.com/udhdhmi/videos/rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:@"item"]) {
model = [[VideoModel alloc]init];
}
if ([element isEqual:@"media:thumbnail"]) {
NSString *imageURLString = [attributeDict objectForKey:@"url"];
[model.videoDescription appendString:imageURLString];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:@"title"]) {
[model.videoTitle appendString:string];
}
if ([element isEqualToString:@"pubDate"]) {
[model.videoPubDate appendString:string];
}
if ([element isEqualToString:@"link"]) {
[model.videoLink appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[feeds addObject:model];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
[self.tableView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}