我正在研究一個應用程序,通過xml獲取一堆描述,然後解析後將它們放在屏幕上。我在撇號變成問號時遇到了一些問題。我的意思是,他們從xml開始,在輸出屏幕和數據庫中,我將它們作爲一個撇號,然後作爲問號顯示在應用程序中。這並不總是會發生,但每次都會有相同的描述。這裏有一個例子:如何解決Ipad/Iphone上無法識別的特殊字符問題?
這是在XML /數據庫
But it won't be easy.
After a tour of the house, you'll be
這是在應用程序中顯示的內容:
But it won?t be easy.
After a tour of the house, you?ll be
我敢肯定,問題是, ipad/iphone不能識別它正在接收的字符......但我不知道如何去解決它。這裏是我的解析器代碼:我相信xml是以UTF-8編碼的形式發送的。
[whereToGetXML appendFormat:xmlID];
NSURL *URL=[[NSURL alloc] initWithString:whereToGetXML];
NSData *dataFromServer = [[NSData alloc] initWithContentsOfURL:URL];
NSLog(@"where to get xml is:%@", whereToGetXML);
// Do any additional setup after loading the view from its nib.
NSData *dataWithoutStringTag = [[NSData alloc]init];
NSXMLParser *firstParser = [[NSXMLParser alloc] initWithData: dataFromServer];
[firstParser setDelegate:self];
[firstParser parse];
//Convert the returned parsed string to data using dataUsingEncoding method. HAVE TO HAVE allowLossyConversion:YES or else it will crash on half of the states.
dataWithoutStringTag =[tempParserString dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];
NSString *htmlCode = [[NSString alloc] initWithData:dataWithoutStringTag encoding:NSASCIIStringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:htmlCode];
NSData *finalData = [temp dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:finalData];
// NSLog(@"Data is: %@", dataWithoutStringTag);
//NSXMLParser* parser = [[NSXMLParser alloc] initWithData: dataFromServer];
[parser setDelegate: self];
//[parser setShouldResolveExternalEntities:YES];
[parser parse];
[parser release];
只是試圖改變編碼方法..通常「NSASCIIStringEncoding」應該爲你工作。 –
這就是我用它來編碼它......你的意思是在服務器端嗎? – Rob