如何解析 iPad 的 XML 中的特殊字符?
我在解析包含一些特殊字符(如单引号、双引号('、“”)等)的 xml 文件时遇到问题。我正在使用 NSXMLParser 的 parser:foundCharacters: 在我的代码中收集字符的方法。
<synctext type = "word" >They raced to the park Arthur pointed to a sign "Whats that say" he asked Zoo said DW Easy as pie</synctext>
当我解析并保存 xml 文件上述标签中的文本时,生成的字符串出现在 GDB 中,如下所示
“\n\t\t他们跑到公园,亚瑟指着一个牌子“那是什么意思”,他问动物园说 DW 简单易行”;
观察到有两个问题:
1)字符串开头有不需要的字符。
2)Whats that say 周围的双引号。
任何人都可以帮助我如何摆脱这些不需要的字符以及如何正确读取特殊字符。
I am getting problem while parsing xml files that contains some special characters like single quote,double quote (', "")etc.I am using NSXMLParser
's parser:foundCharacters:
method to collect characters in my code.
<synctext type = "word" >They raced to the park Arthur pointed to a sign "Whats that say" he asked Zoo said DW Easy as pie</synctext>
When i parse and save the text from above tag of my xml file,the resultant string is appearing,in GDB, as
"\n\t\tThey raced to the park Arthur pointed to a sign \"Whats that say\" he asked Zoo said DW Easy as pie";
Observe there are 2 issues:
1)Unwanted characters at the beginning of the string.
2)The double quotes around Whats that say.
Can any one please help me how to get rid of these unwanted characters and how to read special characters properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSString*string =[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
NSString*string =[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
解析器显然准确地返回了字符串中的内容。也就是说,XML 是用一行上的起始标记、一个换行符、两个制表符和字符串的开头来编码的。字符串中的引号显然在原始文件中存在(并且至少在这个示例中不清楚为什么要删除它们)。
但如果您希望这些字符消失,那么您需要对字符串进行后处理。您可以使用 Rams 的语句来消除换行符和制表符,并使用 stringByReplacingOccurrencesOfString:WithString: 来消除引号。
(请注意,可以指示某些 XML 解析器返回这样的字符串,并删除前导/尾随内容,但我不确定这一点。不过,引号将始终存在。)
The parser is apparently returning exactly what's in the string. That is, the XML was coded with the starting tag on one line, a newline, two tabs, and the start of the string. And quotes in the string are obviously there in the original (and it's not clear in at least this example why you'd want to delete them).
But if you want these characters gone then you need to post-process the string. You can use Rams' statement to eliminate the newline and tabs, and stringByReplacingOccurrencesOfString:WithString: to zap the quotes.
(Note that some XML parsers can be instructed to return strings like this with the leading/trailing stuff stripped, but I'm not sure about this one. The quotes will always be there, though.)