标签:
1. 把请求的数据转化成data
2.取XML文件的前40个字节
NSData * xmldata = [self.data subdataWithRange:NSMakeRange(0,40)];
3.以UTF-8编码进行解码
NSString *xmlstr = [[NSString alloc] initWithData:xmldata encoding:NSUTF8StringEncoding];
//搜索GB2312,如果找到,就把GB2312 改成 utf-8 (ps:替换字符串)
if ([xmlstr rangeOfString:@"\"GB2312\"" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(@"遇到:GB2312 encoding founded.正在把GB2312改成utf-8");
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *utf8str = [[NSString alloc] initWithData:self.data encoding:enc];
utf8str = [utf8str stringByReplacingOccurrencesOfString:@"\"GB2312\"" withString:@"\"utf-8\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,40)];
//新的数据,可以进行正常解析
NSData *newData = [utf8str dataUsingEncoding:NSUTF8StringEncoding];
}
标签:
原文地址:http://www.cnblogs.com/benxiaokang/p/5239734.html