如何修复Ipad / Iphone上无法识别的特殊字符的问题?

时间:2011-11-18 23:57:32

标签: iphone objective-c xml-parsing

我正在开发一个应用程序,它通过xml获取大量描述,然后解析后将它们放在屏幕上。我有一些问题,有些撇号变成了问号。我的意思是,他们从xml开始,在输出屏幕和数据库中我从撇号开始,然后它作为问号显示在应用程序上。这并不总是会发生,但每次都会使用相同的描述。下面是一个例子:

这是xml / database中的内容

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无法识别它正在接收的角色......但我不知道如何修复它。这是我的Parser代码:我相信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];

1 个答案:

答案 0 :(得分:0)

这可能是因为字符串没有被转义。

尝试在每个引号前添加反斜杠。

\'message\'