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

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

2012-01-08 03:19 363 查看
iPhone 应用里解析 XML时遇到空格等特殊符号容易出错,最彻底的解决方法就是将其过滤,下面这段代码不是最优的,但能给大家一点提示
//去特殊符号

- (NSMutableData *)replaceHtmlEntities:(NSMutableData *)data

{

NSString*htmlCode = [[NSString alloc] initWithData:dataencoding:NSISOLatin1StringEncoding];

NSMutableString*temp = [NSMutableString stringWithString:htmlCode];

[tempreplaceOccurrencesOfString:@"&"withString:@"&"
options:NSLiteralSearchrange:NSMakeRange(0, [temp length])];

[tempreplaceOccurrencesOfString:@" "
withString:@" "options:NSLiteralSearch range:NSMakeRange(0, [temp length])];

[tempreplaceOccurrencesOfString:@"À" withString:@"à"options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];

NSData*finalData = [tempdataUsingEncoding:NSISOLatin1StringEncoding];

[datasetData:finalData];

[htmlCoderelease];

returndata;

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