NSXMLParser 用 unicode 字符分割字符串 [英] NSXMLParser divides string with unicode characters
问题描述
我正在使用 NSXMLParser 从以下 xml 代码中获取以下字符串(sqrt(2 x)^(7/6)×(7/6)/(4/9)):
I am using NSXMLParser to get the following string(sqrt(2 x)^(7/6)×(7/6)/(4/9)) from the following xml code:
<pod title='Input'
scanner='Identity'
id='Input'
position='100'
error='false'
numsubpods='1'>
<subpod title=''>
<plaintext>sqrt(2 x)^(7/6)×(7/6)/(4/9)</plaintext>
</subpod>
</pod>
但输出字符串被 foundCharacters 函数分成两个单独的字符串,该函数返回以下结果(使用 NSLog):
But the output string is being divided into two separate strings by the foundCharacters function which returns the following result (using NSLog):
2011-11-11 18:29:46.005 Wolfram API-test[35959:f803] sqrt(2 x)^(7/6)2011-11-11 18:29:46.005 Wolfram API 测试 [35959:f803] ×(7/6)/(4/9)
2011-11-11 18:29:46.005 Wolfram API-test[35959:f803] sqrt(2 x)^(7/6) 2011-11-11 18:29:46.005 Wolfram API-test[35959:f803] ×(7/6)/(4/9)
解析代码如下:
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string{
if (elementFound) {
[string canBeConvertedToEncoding:NSASCIIStringEncoding];
NSLog(@"%@", string);
}
}
所以我的问题是为什么输出字符串被分成两个单独的字符串?
So my question is why the output string is being divided into two separate strings?
推荐答案
我不知道为什么在这种情况下它会被划分,但总的来说,你不应该期望从 -parser:foundCharacters 获得的数据:
一次性全部返回.相反,您应该将从该方法获得的字符累积到一个字符串中.然后,当您的解析器委托被通知在 -parser:didEndElement:...
I'm not sure why it's being divided in this case, but in general you shouldn't expect the data you get from -parser:foundCharacters:
to all be returned at once. Instead, you should accumulate the characters you get from that method in a string. You can then do something when your parser delegate is notified that the end tag has been received in -parser:didEndElement:...
unicode 字符可能是也可能不是您的数据被拆分的原因,但是否与您的代码无关.
The unicode character may or may not be the reason that your data was split up, but whether it was or wasn't shouldn't matter to your code.
这篇关于NSXMLParser 用 unicode 字符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!