你好,这是我在一个字符串中的xml ......
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><CelsiusToFahrenheitResponse
xmlns="http://tempuri.org/"><CelsiusToFahrenheitResult>73.4</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
我希望73.4来自<CelsiusToFahrenheitResult>73.4</CelsiusToFahrenheitResult>
...是否有任何最快的方法使用字符串函数来执行此操作? ......不想遍历整个xml!
答案 0 :(得分:1)
不使用xml-parser的最简单的解决方案是使用NSRegularExpression
。像这样:
NSString *pattern = @"<CelsiusToFahrenheitResult>(.*)</CelsiusToFahrenheitResult>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
__block NSString *fahrenheitString = nil;
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
if (0 < [match numberOfRanges]) {
NSRange range = [match rangeAtIndex:1];
fahrenheitString = [yourString substringWithRange:range];
}
}];