NSRegularExpression para extrair texto entre duas tags XML
Como extrair o valor "6" entre as tags "badgeCount" usando NSRegularExpression. A seguir está a resposta do servidor:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName>
A seguir é o código que tentei, mas não obtendo sucesso. Na verdade, ele entra em outra parte e imprime "O valor da regex é nulo":
NSString *responseString = [[NSString alloc] initWithBytes:[responseDataForCrntUser bytes] length:responseDataForCrntUser.length encoding:NSUTF8StringEncoding];
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=badgeCount>)(?:[^])*?(?=</badgeCount)" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:responseString options:0 range:NSMakeRange(0, [responseString length])];
NSLog(@"NOT NIL");
if (firstMatch) {
NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
NSString *value = [urlString substringWithRange:accessTokenRange];
NSLog(@"Value: %@", value);
}
}
else
NSLog(@"Value of regex is nil");
Se você pudesse fornecer um código de exemplo que seria muito apreciad
NOTA: Não quero usar o NSXMLParser.