您的位置:首页 > 编程语言

解析XML时过滤空格等特殊符号防止出错的代码

2010-09-27 06:36 417 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: