要解析xml文件,当然要先了解一下什么是xml文件啦,在这里就不说了,接上一篇文章,由于是要获取新浪的新闻,我是新浪的rss中心里面找到相应的新闻类别,比如 体育新闻,然后点击xml,此时打开的是浏览器显示的xml文件,而我们要解析这个文件,是需要看它的源代码的,看看主要是有哪些标签,我要提取的主要是新闻的标题,发表时间,以及详细内容的链接,所以就找出这三个标签,分别是title、pubdate、link。
objective-c解析xml文件在网上有很多很强大的第三方库,因为我要的功能很简单,所以只需要用NSXMLParser来解析就可以了,在这个类的delegate中,有三个重要的函数需要去实现
#pragma NSXMLParserDelegate -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"title"]) { if(self.newsTitle == nil) self.newsTitle = [[NSMutableArray alloc]init]; } else if([elementName isEqualToString:@"link"]) { if(self.newsLink == nil) self.newsLink = [[NSMutableArray alloc]init]; } else if([elementName isEqualToString:@"pubDate"]) { if(self.newsPubdate == nil) self.newsPubdate = [[NSMutableArray alloc]init]; } } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(self.tempString == nil) self.tempString = [[NSMutableString alloc]init]; [self.tempString appendString:string]; } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"title"]) { [self.newsTitle addObject:self.tempString]; } else if([elementName isEqualToString:@"link"]) { [self.newsLink addObject:self.tempString]; } else if([elementName isEqualToString:@"pubDate"]) { [self.newsPubdate addObject:self.tempString]; } self.tempString = nil; }
实现了这三个函数,一俄就可以解析xml文件了。
当然要实现上面的三个函数,需要将我现在的这个类作为NSXMLParserDelegate的delegator,而且在创建完NSXMLparser的对象后,要调用setDelegate方法,声明自己就是delegator,然后再调用parser方法,就会开始解析xml文件了,也就是反复调用上面的三个函数。
好了,下面是这个UITbaleViewController的完整代码
// // sportNewsTableViewController.m // myNewsApplication // // Created by mac11 on 12-3-22. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "sportNewsTableViewController.h" #import "newsWebViewController.h" @interface sportNewsTableViewController() <NSXMLParserDelegate> @property (nonatomic,strong) NSMutableArray *newsTitle; @property (nonatomic,strong) NSMutableArray *newsLink; @property (nonatomic,strong) NSMutableArray *newsPubdate; @property (nonatomic,strong) NSMutableString *tempString; @end @implementation sportNewsTableViewController @synthesize newsTitle = _newsTitle; @synthesize newsLink = _newsLink; @synthesize newsPubdate = _newsPubdate; @synthesize tempString = _tempString; #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://rss.sina.com.cn/roll/sports/hot_roll.xml"]]; self.tempString = nil; [xmlParser setDelegate:self]; [xmlParser parse]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma NSXMLParserDelegate -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"title"]) { if(self.newsTitle == nil) self.newsTitle = [[NSMutableArray alloc]init]; } else if([elementName isEqualToString:@"link"]) { if(self.newsLink == nil) self.newsLink = [[NSMutableArray alloc]init]; } else if([elementName isEqualToString:@"pubDate"]) { if(self.newsPubdate == nil) self.newsPubdate = [[NSMutableArray alloc]init]; } } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(self.tempString == nil) self.tempString = [[NSMutableString alloc]init]; [self.tempString appendString:string]; } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"title"]) { [self.newsTitle addObject:self.tempString]; } else if([elementName isEqualToString:@"link"]) { [self.newsLink addObject:self.tempString]; } else if([elementName isEqualToString:@"pubDate"]) { [self.newsPubdate addObject:self.tempString]; } self.tempString = nil; } #pragma mark - sportNewsTableViewControllerDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ([self.newsPubdate count]-2); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"sportsNewsCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... NSString *title = [self.newsTitle objectAtIndex:indexPath.row+2]; NSString *pubdate = [self.newsPubdate objectAtIndex:indexPath.row+1]; cell.textLabel.text = title; cell.detailTextLabel.text = pubdate; return cell; } #pragma mark - segue -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //do someting for the news content if([segue.identifier isEqualToString:@"sportNews"]) { NSMutableString *str = [[NSMutableString alloc] init]; [str appendString:[self.newsLink objectAtIndex:self.tableView.indexPathForSelectedRow.row+2]]; [str deleteCharactersInRange:NSMakeRange(0, 4)]; [segue.destinationViewController setWebURL:str]; [segue.destinationViewController setNewsTitle:@"体育新闻"]; } } @end