Two ways to parse HTML for iOS

Source: Internet
Author: User
Tags call back cdata tagname

1. Recently did not have to parse the HTML text, in the parsing will encounter some problems, but now also solved, I used two ways to parse the HTML page, now say what is the way to parse

The first method: use regular Expressions (Http://rss.sina.com.cn/sports/basketball/nba.xml the data that needs to be parsed)

Use multithreading to parse HTML data:

-(void) getnews{

Using multithreaded development

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

The requested 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 (), ^{//method to return to the main thread

NSLog (@ "Call back the data is:%@", HTMLSTR);

[Self updatview:htmlstr];

});

});

}

<item> the data that needs to be parsed

<title>

<! [cdata[warm-up-world wave equaliser after the lore of AC Milan 1-2 negative Lyon]]>

</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[international football-Focus News]]>

</category>

<pubdate>sat, 20:40:57 gmt</pubdate>

<comments>http://comment.news.sina.com.cn/cgi-bin/comment/comment.cgi?channel=gn&amp;newsid= 6-12-4810592</comments>

<description>

<! [cdata[]]>

</description>

</item>

-(void) Updatview: (NSString *) htmlstr{

Data source for Title

NSString *copy = htmlstr;

int start = 0;

while (YES) {

Start from that position.

HTMLSTR = [Htmlstr Substringfromindex:start];

Set a flag

NSString *tag = @ "cdata\\[.*\\]";

Set a range (regular expression method)

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:@ "international football-Focus News]") {

Continue

}

else{

[Self.titlearrray Addobject:find];

}

}else{

Break

}

}

Urlarray

htmlstr = copy;

int start2 = 0;

while (YES) {

1 start from that position

HTMLSTR = [Htmlstr substringfromindex:start2];

2. Set a tag

NSString *tag2 = @ "<link>.*</link>";

3. Set a range

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 start from that position

HTMLSTR = [Htmlstr substringfromindex:start3];

2. Set a tag

NSString *tag3 = @ "<pubDate>.*</pubDate>";

3. Set a range

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];

}

The second way: (using third-party library 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];

}

}

There is a problem here, I took the data last night is always out, the teacher depressed Ah, the original is a lot of analytical data inside the space and line, to remove the line can be implemented data loading:

-(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]]; Remove end-of-line whitespace characters and newline characters

Headerdata = [Headerdata stringbyreplacingoccurrencesofstring:@ "\ r" withstring:@ ""];

Headerdata = [Headerdata stringbyreplacingoccurrencesofstring:@ "\ n" withstring:@ ""];

Cell.textLabel.text = Headerdata;

Cell.textLabel.numberOfLines = 0;

return cell;

}

Two ways to parse HTML for iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.