標籤:
1.最近沒什麼做的就解析了html的文本來了,在解析的時候會遇到一些問題,但是現在也解決了, 我使用了兩種方式去解析Html 頁面,現在就來說一下是什麼方式去解析的
第一種的方法:使用Regex(http://rss.sina.com.cn/sports/basketball/nba.xml 需要解析的資料)
使用多線程的方式去解析html資料:
-(void)getNews{
//使用多線程開發
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//請求的url
NSString *requestStr = @"http://rss.sina.com.cn/sports/global/focus.xml";
NSString *htmlStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:requestStr] encoding:NSUTF8StringEncoding error:nil];
dispatch_async(dispatch_get_main_queue(), ^{ //回到主線程的方法
NSLog(@"call back the data is :%@",htmlStr);
[self updatview:htmlStr];
});
});
}
//<item>需要要解析的資料
//<title>
//<![CDATA[熱身-世界波扳平後遭絕殺 AC米蘭1-2客負裡昂]]>
//</title>
//<link>http://go.rss.sina.com.cn/redirect.php?url=http://sports.sina.com.cn/g/seriea/2015-07-19/04407658051.shtml</link>
//<author>WWW.SINA.COM.CN</author>
//<guid>http://go.rss.sina.com.cn/redirect.php?url=http://sports.sina.com.cn/g/seriea/2015-07-19/04407658051.shtml</guid>
//<category>
//<![CDATA[國際足壇-焦點新聞]]>
//</category>
//<pubDate>Sat, 18 Jul 2015 20:40:57 GMT</pubDate>
//<comments>http://comment.news.sina.com.cn/cgi-bin/comment/comment.cgi?channel=gn&newsid=6-12-4810592</comments>
//<description>
//<![CDATA[]]>
//</description>
//</item>
-(void)updatview:(NSString *)htmlStr{
//title的資料來源
NSString *copy = htmlStr;
int start = 0;
while (YES) {
//從那個位置開始
htmlStr = [htmlStr substringFromIndex:start];
//設定一個標誌
NSString *tag = @"CDATA\\[.*\\]";
//設定一個範圍(Regex方法)
NSRange rang = [htmlStr rangeOfString:tag options:NSRegularExpressionSearch];
if (rang.length >0) {
NSString *find = [htmlStr substringWithRange:rang];
find = [[find stringByReplacingOccurrencesOfString:@"CDATA[" withString:@""]stringByReplacingOccurrencesOfString:@"]" withString:@""];
NSLog(@"find :%@",find);
start = (int)(rang.location +rang.length);
if ([find isEqualToString:@""]||[find isEqualToString:@"國際足壇-焦點新聞"]) {
continue;
}
else{
[self.titleArrray addObject:find];
}
}else{
break;
}
}
//urlArray
htmlStr = copy;
int start2 = 0;
while (YES) {
//1從那個位置開始
htmlStr = [htmlStr substringFromIndex:start2];
//2.設定一個tag
NSString *tag2 = @"<link>.*</link>";
//3.設定一個範圍
NSRange rang2 = [htmlStr rangeOfString:tag2 options:NSRegularExpressionSearch];
if (rang2.length > 0) {
NSString *find2 = [htmlStr substringWithRange:rang2];
find2 = [[find2 stringByReplacingOccurrencesOfString:@"<link>" withString:@""]stringByReplacingOccurrencesOfString:@"</link>" withString:@""];
[self.urlArray addObject:find2];
NSLog(@"find2:%@",find2);
start2 = (int)(rang2.location +rang2.length);
}else {
break;
}
}
//timeArray
htmlStr = copy;
int start3 = 0;
while (YES) {
//1從那個位置開始
htmlStr = [htmlStr substringFromIndex:start3];
//2.設定一個tag
NSString *tag3 = @"<pubDate>.*</pubDate>";
//3.設定一個範圍
NSRange rang3 = [htmlStr rangeOfString:tag3 options:NSRegularExpressionSearch];
if (rang3.length > 0) {
NSString *find3 = [htmlStr substringWithRange:rang3];
find3 = [[find3 stringByReplacingOccurrencesOfString:@"<pubDate>" withString:@""]stringByReplacingOccurrencesOfString:@"</pubDate>" withString:@""];
[self.dataArray addObject:find3];
NSLog(@"find2:%@",find3);
start3 = (int)(rang3.location +rang3.length);
}else {
break;
}
}
[self.titleArrray removeObjectAtIndex:0];
[self.titleArrray removeObjectAtIndex:0];
[self.titleArrray removeObjectAtIndex:0];
[self.urlArray removeObjectAtIndex:0];
[self.urlArray removeObjectAtIndex:0];
[self.urlArray removeObjectAtIndex:0];
[self.dataArray removeObjectAtIndex:0];
NSLog(@"%@,%@,%@" ,self.titleArrray , self.urlArray, self.dataArray);
[self.NewTableView reloadData];
}
第二中方式:(使用了第三方庫TFHpple)
- (void)jiexi {
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://rss.sina.com.cn/sports/basketball/nba.xml"]];
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
TFHpple *doc=[[TFHpple alloc] initWithXMLData:data];
NSArray *items=[doc searchWithXPathQuery:@"//item"];
self.articles=[NSMutableArray array];
Article *aricle=nil;
for (TFHppleElement *item in items) {
aricle=[[Article alloc] init];
for (TFHppleElement *element in item.children) {
if ([@"title" isEqualToString:element.tagName]) {
aricle.title=element.content;
}else if ([@"link" isEqualToString:element.tagName]){
aricle.link=element.content;
}
}
[self.articles addObject:aricle.title];
}
}
在這裡有問題了,我昨晚取到資料老是出不來,老師鬱悶呀,原來是解析的資料裡面好多的空格和換行了,要去掉換行就可以實現了資料的載入:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.articles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSString * string = [self.articles objectAtIndex:indexPath.row];
NSString* headerData=string;
headerData = [headerData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字元和換行字元
headerData = [headerData stringByReplacingOccurrencesOfString:@"\r" withString:@""];
headerData = [headerData stringByReplacingOccurrencesOfString:@"\n" withString:@""];
cell.textLabel.text = headerData;
cell.textLabel.numberOfLines = 0;
return cell;
}
IOS之解析Html的兩種方式