1

如何解析 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>

有任何想法吗?提前致谢。

4

4 回答 4

1

为此,您需要 XML 解析器。

这里有教程

于 2013-05-15T13:21:42.130 回答
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

于 2013-05-15T13:33:54.863 回答
0

对于这种特殊情况,您可以获得 and 的范围<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。

于 2013-05-15T13:23:05.427 回答
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);
        }
    }

希望这会对您有所帮助更多请尝试此博客Git 项目资源可能会帮助您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;
}
于 2013-05-15T13:31:28.370 回答