rss解析

rss数据格式如下:

 




<![CDATA[国际要闻-新浪新闻]]>



<![CDATA[新闻中心-国际新闻]]>

http://news.sina.com.cn/world
http://www.sinaimg.cn/home/deco/2009/0330/logo_home_news.gif




http://news.sina.com.cn/491/2008/0827/1.html
zh-cn
WWW.SINA.COM.CN
5



Thu, 28 Jun 2012 13:57:02 GMT





<![CDATA[俄罗斯称不准备在叙利亚问题采取特别措施]]>

http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/184124676271.shtml
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/184124676271.shtml



Thu, 28 Jun 2012 10:41:57 GMT







<![CDATA[瑞典男子怀疑妻子红杏出墙吃掉其嘴唇]]>

http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/180524676120.shtml
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/180524676120.shtml



Thu, 28 Jun 2012 10:05:25 GMT







<![CDATA[视频:日本萌系电视台女主播网络爆红]]>

http://go.rss.sina.com.cn/redirect.php?url=http://video.sina.com.cn/p/news/w/v/2012-06-28/180461790937.html
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://video.sina.com.cn/p/news/w/v/2012-06-28/180461790937.html



Thu, 28 Jun 2012 10:04:30 GMT







<![CDATA[高清图:微软希腊分公司遭恐怖袭击暂停运营]]>

http://go.rss.sina.com.cn/redirect.php?url=http://slide.news.sina.com.cn/w/slide_1_2841_24419.html
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://slide.news.sina.com.cn/w/slide_1_2841_24419.html



Thu, 28 Jun 2012 10:00:23 GMT







<![CDATA[欧洲科学家发现猴面兰花(图)]]>

http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/p/2012-06-28/174824676055.shtml
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/p/2012-06-28/174824676055.shtml



Thu, 28 Jun 2012 09:48:40 GMT







<![CDATA[叙利亚反对派拒绝履行和平计划 除非阿萨德下台]]>

http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/173524676076.shtml
WWW.SINA.COM.CN
http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2012-06-28/173524676076.shtml



Thu, 28 Jun 2012 09:35:00 GMT






 

具体的解析方式如下:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
	self.title = @"国际要闻";	
	self.rssList = [[NSMutableArray alloc] init];
	
	NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://rss.sina.com.cn/news/world/focus15.xml"]];
	parser.delegate = self;
	[parser parse];	
}

#pragma mark -
#pragma mark NSXMLParser delegate Methods

- (void)parserDidStartDocument:(NSXMLParser *)parser {
	NSLog(@"startParser");	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	[parser release];
	NSLog(@"%@", rssList);	
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if([elementName isEqualToString:@"item"]) {
		currentItem = [[NSMutableDictionary alloc] init];
	} else if (currentItem != NULL) {
		currentContents = [[NSMutableString alloc] init];
	}	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	if([elementName isEqualToString:@"item"]) {
		[rssList addObject:currentItem];
		[currentItem release];
	} else if (currentContents && currentItem) {
		[currentItem setObject:currentContents forKey:elementName];
		[currentContents release];
		currentContents = nil;		
	}	
}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {
	if(currentItem && currentContents) {
		currentContents = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if(currentItem && currentContents) {
		[currentContents appendString:string];		
	}	
}

你可能感兴趣的:(iOS)