如何解决 Ipad/Iphone 上无法识别的特殊字符的问题?
我正在开发一个应用程序,它通过 xml 获取大量描述,然后在解析后将它们显示在屏幕上。我对某些描述中的撇号变成问号有疑问。我的意思是,它们从 xml 开始,在输出屏幕和数据库中我以撇号的形式获取它们,但随后它在应用程序上显示为问号。这种情况并不总是发生,但每次都会出现相同的描述。这是一个例子:
这是 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];
I am working on an app that gets a bunch of descriptions through xml and then after parsing puts them on the screen. I'm having a problem with some of the descriptions with apostrophes turning into question marks. What I mean is, they start out in the xml, on the output screen and in the database I get them from as an apostrophe, but then it shows up on the app as a Question mark. This doesn't always happen, but it does with the same descriptions every time. Heres an example:
This is what is in the xml/database
But it won't be easy.
After a tour of the house, you'll be
This is what shows up on the app:
But it won?t be easy.
After a tour of the house, you?ll be
I'm pretty sure that the problem is that the ipad/iphone doesn't recognize the character that it is receiving...but I have no idea how I would go about fixing it. Here is a my Parser code: I believe the xml is being sent as UTF - 8 encoding.
[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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为字符串没有转义。
尝试在每个引号前添加反斜杠。
It's presumably because the string isn't escaped.
Try adding a backslash before each quotation mark.