我想解码一个xml文件。url是http://bbs.byhh.net/morecommend.xml这个文件是用gb2312编码的。所以我只是把编码方式从gb2312转为UTF-8。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlStr = @"http://bbs.byhh.net/morecommend.xml";
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//NSLog(@"%@", returnData);
NSString *results = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:0x80000632];//gb2312 0x80000632 where the string becomes null
NSString *newresults = [results stringByReplacingOccurrencesOfString:@"gb2312" withString:@"UTF-8"];
NSData *newData = [newresults dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"results=%@",new);
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:newData];
BOOL suc = [xmlParser parse];
if (suc == YES) {
NSLog(@"success");
}
else{
NSLog(@"fail");
}
[results release];
[request release];
奇怪的是,我确实收到了返回数据。我也尝试将其写入文件。该文件是正确格式的正确 xml 文件。但是当我将数据传输到字符串中时。字符串变为空。我不知道出了什么问题。