Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

parsing HTML Content

Hello to Community


I have NSString str, that contains html content with tags etc lokks like this

<div class="top-bar"><div class="top-bar-inner page-width-content"><div class="account-bar"><div class="inline-block"><div class="my-account-btn"><div class="my-account-label"><img src="../gui_lib/images/cleardot.gif" class="icon account-icon" title="my_account" /><label><a class="label" href="account.php">Hello Anton,<span class="h-spacer"></span>(Account 3894501)</a></label><span class="h-spacer"></span><a style="text-decoration:underline;" href="login.php?action=logout" />Logout</a></div></div></div></div><div class="user-bar"></div><form class="language-selector inline-block" dir="ltr" method="post" action="../rpc/change_lang.rpc.php"><button class="selected" type="submit" name="ui_lang" value="en_UK"><span class="button-label">English</span></button><span class="language-selector-seperator"> | </span><button type="submit" name="ui_lang" value="es_ES"><span class="button-label">Español</span></button><span class="language-selector-seperator"> | </span><button type="submit" name="ui_lang" value="fr_FR"><span class="button-label">Français</span></button><span class="language-selector-seperator"> | </span><button type="submit" name="ui_lang" value="ar_EG"


I need parse all content in str (NSString) and get only data I marked in red



How I can to that correctly?


Thanks

iPhone 5, iOS 6.1.3

Posted on Apr 11, 2013 12:02 PM

Reply
Question marked as Best reply

Posted on Apr 14, 2013 11:14 AM

One way to parse data from HTML strings is to use regular expressions with NSRegularExpression. This is very error prone unless the HTML layout is predictable and stable. If you control the HTML layout (i.e the web app) you can insert identifiers (such as CSS class names) to make it easy to parse. If you don't control the HTML layout, you need to get really good at regular expression syntax before trying this. There are plenty of online regular expression testers on the net, use a regex tester before wasting time writing Objective-C code.



In this example routerStatusReponse is an HTML string.


wanConnectionType = [NSRegularExpressionstringMatchInString:routerStatusResponse pattern:@"Broadband Connection Type.*?<td[^>]*>([^<]*)<"];


NSRegularExpression stringMatchInString is a class extension (category).


+ (NSString *)stringMatchInString:(NSString *)string pattern:(NSString *)pattern {

if (string) {

NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitiveerror:&error];

NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];

if (match)

return [string substringWithRange:[match rangeAtIndex:1]];

}

returnnil;

}

2 replies
Question marked as Best reply

Apr 14, 2013 11:14 AM in response to !#

One way to parse data from HTML strings is to use regular expressions with NSRegularExpression. This is very error prone unless the HTML layout is predictable and stable. If you control the HTML layout (i.e the web app) you can insert identifiers (such as CSS class names) to make it easy to parse. If you don't control the HTML layout, you need to get really good at regular expression syntax before trying this. There are plenty of online regular expression testers on the net, use a regex tester before wasting time writing Objective-C code.



In this example routerStatusReponse is an HTML string.


wanConnectionType = [NSRegularExpressionstringMatchInString:routerStatusResponse pattern:@"Broadband Connection Type.*?<td[^>]*>([^<]*)<"];


NSRegularExpression stringMatchInString is a class extension (category).


+ (NSString *)stringMatchInString:(NSString *)string pattern:(NSString *)pattern {

if (string) {

NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitiveerror:&error];

NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];

if (match)

return [string substringWithRange:[match rangeAtIndex:1]];

}

returnnil;

}

parsing HTML Content

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.