3

I don't know how to parse HTML page content in iPhone SDK?

I have an iPhone app, in this app I need to show the image and data from the HTML page. I have an HTML page URL, I need to get data from the HTML page URL. Can anyone please guide me to parse HTML content from HTML page and show in iPhone app?

Can you please help me?

EDIT

I have an website in HTML format like this http://www.example.com/mobile/403.html page. I want to develop a native iPhone app for this website. My client wont give the response in XML feed so i need to use this site and parse the HTML contents. This page having many images, live data and tables. Till now i didn't parsed HTML page/content in iPhone SDK. So i need your help to do this? Can you please help me? I hope it is clear comparing with old question. Thanks in advance.

Gopinath
  • 5,392
  • 21
  • 64
  • 97
  • directly load that url in webview – Narayana Rao Routhu Jun 01 '12 at 11:55
  • @Narayana I can't load it in UIWebview. I need to parse the data from HTML page and load using iOS way. Can you please help me? – Gopinath Jun 01 '12 at 11:58
  • Have you tried searching StackOverflow first? http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – benwong Jun 01 '12 at 12:00
  • Do you have a server that you can deploy a web service to that you could have scrape the HTML page into what you need and provide that data to the iPhone via JSON, XML, etc.? – mccrager Jun 01 '12 at 12:27
  • Thanks Mr.Mccrager. I don't have a server to scrape the HTML page into XML/JSon format. My client gave a link in HTML i need to parse and load in iOS way for iPhone app. Thanks in advance. – Gopinath Jun 01 '12 at 12:29
  • @benwong Yes i have visited that link and downloaded the sample from github.com. Am trying that code also. Is there any way to parse http://www.example.com/mobile/403.html url in iphone and get the contents. Thanks in advance. – Gopinath Jun 01 '12 at 12:34
  • What do you mean by "Load in iOS way". If you're going to load the page, what's the point to parse it first? Parsing and loading aren't the same thing. You also say you need to get the html data? What do yo u want to do: get the content data of the html page, parse the html page, load the url or load the html page into a browser. All of these are 4 different things and its not clear which of them you want to do. Make your question more precise and clear. – Gruntcakes Jun 01 '12 at 13:53
  • @MartinH I thought that the HTML page contains live data and many images. Our client wants iPhone app from that HTML page so we need to parse HTML content and show with using our native iOS tools. Thank you Mr.Martin. I will do it now. Thanks. – Gopinath Jun 01 '12 at 14:01
  • @MartinH I have edited my question. Can you please guide me? Thanks in advance. – Gopinath Jun 01 '12 at 14:08
  • Parsing a HTML page is extracting the information and data from it. Once you have got that information and data you then need to do something with it. A web browser will parse the page to get the data and then display it. It is still not clear from your question if you just want to parse it, or parse it and display it, or actually you just want to display the web page. If you just want to display the page you have two choices: launch Safari from your app, or use UIWebView to display it. – Gruntcakes Jun 01 '12 at 14:28

3 Answers3

2

You can add the the image url to the NSMutableArray as code below..

     NSMutableArray *array = [[NSMutableArray alloc] init];
     NSString *response = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index1" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
  //  NSLog(@"response == %@", response);
   NSString *regexStr = @"<a href=\"([^>]*)\">";
  //NSString *regexStr = @"<A HREF=\"([^>]*)\">";
    NSError *error;
    NSInteger i =0;
 // NSInteger length =0;
    while (i<[response length]) {
           NSRegularExpression *testRegex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
        if( testRegex == nil ) NSLog( @"Error making regex: %@", error );
            NSTextCheckingResult *result = [testRegex firstMatchInString:response options:0 range:NSMakeRange(i, [response length]-i)];
 //   NSLog(@"result == %@",result);
            NSRange range = [result rangeAtIndex:1];
            if (range.location == 0) {
            break;
        }
        NSString * imageUrl = [response substringWithRange:range];
        if ([imageUrl hasSuffix:@".jpg"] || [imageUrl hasSuffix:@".gif"] || [imageUrl hasSuffix:@".tiff"] || [imageUrl hasSuffix:@".JPG"] || [imageUrl hasSuffix:@".JPEG"] || [imageUrl hasSuffix:@".png"] || [imageUrl hasSuffix:@".PNG"] || [imageUrl hasSuffix:@".GIF"] || [imageUrl hasSuffix:@".TIFF"]) {
       // NSLog(@"%@",imageUrl);
       // imageUrl = [imageUrl stringByReplacingOccurrencesOfString:@"/syneye_Portfolio/" withString:@""];
            [array addObject:imageUrl];
            //[array retain];
        }
        i= range.location;
        //NSLog(@"%i",range.location);
        i=i+range.length;
    }
Shorhashi
  • 670
  • 5
  • 8
  • Thank you Shorhashi. Can i use HTML page URL in this code? Like http://www.example.com/mobile/403.html how can i parse this link and get content? I will try your code. Can you please help me? Thanks in advance. – Gopinath Jun 01 '12 at 12:32
  • Sorry for the late replay Gopinath...yeah you can also use HTML page URL in this code..like NSURL * url = [NSURL URLWithString:@"http://www.iana.org/domains/example/"]; NSData * data = [[NSData alloc] initWithContentsOfURL:url]; NSString * response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; and then use the response string as i specifed code above.. – Shorhashi Jun 07 '12 at 11:08
  • Thank you Shorhashi. I will try your code and let you know the result. Thanks for your help. – Gopinath Jun 09 '12 at 04:23
0

You should get the HTML thanks to a networking framework (AFNetworking for instance) and parse it with one of the following options:

  1. custom code
  2. some HTML parsing framework (i don't know any)
  3. loading the HTML in a hidden UIWebView and doing some Javascript inside with stringByEvaluatingJavaScriptFromString:
Mick F
  • 7,312
  • 6
  • 51
  • 98
0

I still don't understand exactly what you want, but this is what's possible with urls and html pages:

1) The page can be loaded into Safari. Of course a user can do this if the user has the url. But also a native app can launch Safari and supply Safari with the url of the page to load. A native app cannot however launch Safari and give it the actual html page to load, it must the url.

2) A native app can use UIWebView to either a) download and display a html page given a url or: b) If the html exists on the device, then the html page is given to UIWebView then the UIWebView will display it directly.

3) If the intention is to just extract a bit of text or an image or two from the html and then display it,then search for a few postings/tutorials then you can a) load the html page in a hidden UIWebView and use Javascript to access the dom elements or b) if its xhmlt use an xml parser to extract the html tags you are looking for or c) see if there are html parsing frameworks available for html or d) do a hack and string seach for the html tokens in the html directly.

4) Parse the full HTML and display all its contents yourself. Unless this is a very very simple html, without a full set of features and can't handle javascript etc. etc. Do you have a large team and years free in which to write what would effectively be your own browser.

EDIT: You still keep talking about parsing as if parsing is the same thing as displaying it.

If you just want to display the http page at the url use UIWebView. The UIWebView will parse it AND display it. There's no need for you to parse it yourself. Or launch Safari from you app (but you won't be able to return to your app afterwards).

You say you can't use UIWebView? Why not?

To actually try and parse and display the HTML page yourself would be madness.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • Yes Mr.Martin. You are right but my client had a mobile site so he don't want to load the page in uiwebview he wants to parse the data and load it using iOS developing kits. That's y i'm asking to parse HTML data. I hope it is clear. Am not well in english. sorry for my poor english. Thanks in advance. – Gopinath Jun 01 '12 at 14:32
  • I'm sorry I don't know of any existing html parsers. XHMTL could be parsed using any of the XML parsers, I don't know enough about HTML though to know if an XML parser could also be applied to that easily. Is it only for HIS web page that this is to be done for, or it could be for any other page? – Gruntcakes Jun 01 '12 at 14:42
  • Thank you Mr.Martin. I already asked this with client. But he want this way. I don't know how to do this. Thanks for your effort. – Gopinath Jun 01 '12 at 14:44