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

发布于 2024-12-17 07:33:24 字数 1805 浏览 0 评论 0原文

我正在开发一个应用程序,它通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-12-24 07:33:25

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

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

\'message\'

It's presumably because the string isn't escaped.

Try adding a backslash before each quotation mark.

\'message\'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文