iPhone 应用里解析 XML 时遇到空格等特殊符号容易出错,最彻底的解决方法就是将其过滤,下面这段代码不是最优的,但能给大家一点提示
//去特殊符号
- (NSMutableData *)replaceHtmlEntities:(NSMutableData *)data
{
NSString *htmlCode = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:htmlCode];
[temp replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"À" withString:@"à" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
NSData *finalData = [temp dataUsingEncoding:NSISOLatin1StringEncoding];
[data setData:finalData];
[htmlCode release];
return data;
}