2013-05-15 44 views
1

如何解析HTML文件?ios - 如何解析ios中的HTML內容?

我在下面的代碼中獲得一個HTML文件,我只想獲取BinarySecurityToken XML節點之間的數據。

- (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> 

任何想法?提前致謝。

+0

使用我的回答.. –

+0

你有你的答案? –

回答

1

可以得到使用此代碼

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]); 
     } 
    } 

輸出: aHR0cCUzYSUyZiUyZnNjaGVtYWd0Sjk0JTNk

+0

它有一些屬性,也用令牌打印。可以請你幫我刪除它嗎? –

1

你需要和XML解析器。

有一個教程here

+1

+1我同意Hpple(這實際上是鏈接中的主題教程)是一個很好的HTML解析器。我只評論你的答案,因爲一些XML解析器不能很好地處理HTML。但我的經驗是,Hpple做得很好。 – Rob

0

對於這種特殊的情況下,你可以得到的<wsse:BinarySecurityToken></wsse:BinarySecurityToken>範圍,構建新的範圍,將提供令牌的位置,在這個範圍內得到串。

示例代碼:

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]; 

由於您的輸入來自外部的,你應該檢查範圍位置不等於NSNotFound。

+0

感謝您寶貴的回覆,您可以向我展示示例代碼嗎? –

+0

我已經更新了我的回答 – Jeepston

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); 
     } 
    } 

希望這將幫助你For more try this blogGit Project Resource may help youGood blog by RAYWENDERLICH

或其他的選擇,如果你把所有的HTML數據NSString你可以利用這個功能獲得特定NSString之間的數據。

-(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; 
}