Reputation: 5442
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.
Upvotes: 3
Views: 5444
Reputation: 37581
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.
Upvotes: 0
Reputation: 670
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;
}
Upvotes: 2
Reputation: 7439
You should get the HTML thanks to a networking framework (AFNetworking for instance) and parse it with one of the following options:
stringByEvaluatingJavaScriptFromString:
Upvotes: 0