Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dusker

macrumors member
Original poster
May 31, 2009
41
0
Hi Guys!
I'm working on an app of mine, but i came across a problem i find pretty hard to bite. Inside a NSString i'll have a pretty long html code, and i'd like to pull out of that string, substrings that are contained inside certain tags. So for example i'd like to get what's inside <font><b> and </b></font> tags.
There'll be couple of lines containing these tags so i'll need this solution to run inside a loop.

Any help will be greatly appreciated.

greetings
peter
 
@jnic thanks for the clue! Would you (or someone else) care to post a short snippet of what methods to use? Say we got the following:

Code:
NSString *str = @"<html><body><font><b>hello</b></font> <font><b>world</b></font></body></html>";
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:str];

how to get the parser to return what's enclosed inside <font><b> and </b></font>

thanks in advance
 
@jnic thanks for the clue! Would you (or someone else) care to post a short snippet of what methods to use? Say we got the following:

Code:
NSString *str = @"<html><body><font><b>hello</b></font> <font><b>world</b></font></body></html>";
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:str];

how to get the parser to return what's enclosed inside <font><b> and </b></font>

thanks in advance

Check this example:

https://developer.apple.com/iphone/...ts.html#//apple_ref/doc/uid/20002265-BCIJFGJI

in short you would probably want to set up 2 BOOLs like inFont and inB so you know when you are within <font><b>, capture all data until the tags are closed and then clear the BOOLs again
 
use Javascript

Javascript supports regex, and there are several xml/html parsing libraries available for Javascript. You can run the JS code inside a hidden UIWebview. Calling a JS parser from Obj-C might be a lot easier than rolling the equivalent parser in Obj-C.
 
Javascript supports regex, and there are several xml/html parsing libraries available for Javascript. You can run the JS code inside a hidden UIWebview. Calling a JS parser from Obj-C might be a lot easier than rolling the equivalent parser in Obj-C.

thanks for a tip, could you recommend any paticular js html parser?

Well, I'm gonna suggest looking into NSScanner if the structure of what you're pulling from is not overly complex.

I think the html i need to parse is too complex for nsscanner:(

Would anyone care to post some snippet of regex expression that would pull data from inside <font><b> tag?
 
thanks for a tip, could you recommend any paticular js html parser?



I think the html i need to parse is too complex for nsscanner:(

Would anyone care to post some snippet of regex expression that would pull data from inside <font><b> tag?


/<font><b>(.*?)<\/b><\/font>/ism
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.