1

How do I parse HTML file?

I'm getting an HTML file in the below code,I just want to get data in between BinarySecurityToken XML node.

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if(_data) { //Here am getting the below HTML content NSString* content = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]; } } <input type="hidden" name="wa" value="wsignin1.0" /> <input type="hidden" name="wresult" value="<t:RequestSecurityTokenResponse xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"> <t:Lifetime> <wsu:Created >2013-04-29T11:50:29.895Z</wsu:Created> <wsu:Expires>2013-04-29T12:00:29.895Z</wsu:Expires> </t:Lifetime> <wsp:AppliesTo> <EndpointReference> <Address>urn:orin.converse</Address> </EndpointReference></wsp:AppliesTo> <t:RequestedSecurityToken> <wsse:BinarySecurityToken> aHR0cCUzYSUyZiUyZnNjaGVtYWd0Sjk0JTNk </wsse:BinarySecurityToken> 

Any ideas? Thanks in advance.

1
  • Have you got your answer ? Commented May 15, 2013 at 13:46

4 Answers 4

1

You can get using this code

NSRange divRange = [content rangeOfString:@"<wsse:BinarySecurityToken>" options:NSCaseInsensitiveSearch]; if (divRange.location != NSNotFound) { NSRange endDivRange; endDivRange.location = divRange.length + divRange.location; endDivRange.length = [content length] - endDivRange.location; endDivRange = [content rangeOfString:@"</wsse:BinarySecurityToken>" options:NSCaseInsensitiveSearch range:endDivRange]; if (endDivRange.location != NSNotFound) { divRange.location += divRange.length; divRange.length = endDivRange.location - divRange.location; NSLog(@"BinarySecurityToken : %@",[content substringWithRange:divRange]); } } 

Output : aHR0cCUzYSUyZiUyZnNjaGVtYWd0Sjk0JTNk

Sign up to request clarification or add additional context in comments.

1 Comment

<wsse:BinarySecurityToken> it has some attribute,that also printing with the token.Can you please help me to remove that?
1

You need and XML parser for that.

There's a tutorial here

1 Comment

+1 I agree that Hpple (which is really the subject tutorial in your link) is a good HTML parser. I only comment to qualify your answer, because some XML parsers will not handle HTML as gracefully. But my experience is that Hpple does a good job.
0

For this particular case you can get the ranges of <wsse:BinarySecurityToken> and </wsse:BinarySecurityToken>, construct new range that will provide you location of the token, and get substring in that range.

Sample code:

NSRange openingTagRange = [htmlString rangeOfString:@"<wsse:BinarySecurityToken>"]; NSRange closingTagRange = [htmlString rangeOfString:@"</wsse:BinarySecurityToken>"]; NSRange tokenRange = NSMakeRange(openingTagRange.location + openingTagRange.length, closingTagRange.location - (openingTagRange.location + openingTagRange.length)); NSString *token = [htmlString substringWithRange:tokenRange]; 

Since your input comes from outside, you should probably check if the ranges' locations are not equal to NSNotFound.

1 Comment

Thanks for your valuable reply,Can you please show me sample code?
0
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSData * data = [NSData dataWithContentsOfFile:filePath]; TFHpple * tutorialsParser = [[TFHpple alloc] initWithHTMLData:data]; NSString *query = @"//div[@id='BinarySecurityToken']"; NSArray *nodes = [tutorialsParser searchWithXPathQuery:query]; for (TFHppleElement * element in nodes) { NSLog(@"%@", element); NSLog(@"%@", [element tagName]); NSLog(@"%@", [element attributes]); NSLog(@"%@", [element children]); for (TFHppleElement *childElement in [element children]) { NSLog(@"%@", childElement); } } 

hope this will help you For more try this blog and Git Project Resource may help you and Good blog by RAYWENDERLICH

or another option if you have all the HTML data in NSString you can get data between specific NSString with this function.

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString)end { NSRange startRange = [self rangeOfString:start]; if (startRange.location != NSNotFound) { NSRange targetRange; targetRange.location = startRange.location + startRange.length; targetRange.length = [self length] - targetRange.location; NSRange endRange = [self rangeOfString:end options:0 range:targetRange]; if (endRange.location != NSNotFound) { targetRange.length = endRange.location - targetRange.location; return [self substringWithRange:targetRange]; } } return nil; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.