题
我试图阅读Google Weather API的响应数据,但德语Umlauts尚未正确显示。而不是“Ö”我得到“^”。
我认为问题是这两行代码:
CXMLElement *resultElement = [nodes objectAtIndex:0];
description = [[[[resultElement attributeForName:@"data"] stringValue] copy] autorelease];
如果没有StringValue,如何从结果中获取数据?
PS:我使用touchxml来解析xml
解决方案
您必须使用nsurlconnection来获取我想的数据。当您收到数据时,可以使用适当的编码将其转换为NSString。例如
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if(xmlResponse == nil){
xmlResponse = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
else{
NSMutableString *temp = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
[xmlResponse appendString:temp];
[temp release];
}
}
在这里,Xmlresponse是您可以传递给解析器的NSMutableString。我已经使用了nsisolatin1编码。您可以检查其他类型的编码,并查看是什么为您的字符正确提供(我想NSUTF8Stringencoding应该这样做)。您可以检查API DOC中是否有支持的编码列表。
不隶属于 StackOverflow