gpt4 book ai didi

iphone - 难以解析包含 '\u00' 等字符的响应

转载 作者:行者123 更新时间:2023-12-03 21:05:53 25 4
gpt4 key购买 nike

我正在使用 NSXML Parser 在我的 iPhone 应用程序中进行解析。现在一切正常,除非数据是法语。

例如,来自服务器的数据为Ch\u00e9rie FM

现在,在 foundCharacters 方法的 string 参数下,我只得到字符串,因为“Ch”其余字符没有出现。所以最后我的字符串只被截断为“Ch”,而不是整个 Cherie fm

可以做什么?

代码:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if (appDelegate.objPlayer.fromFlickrorRecommend == TRUE)
{
if([elementName isEqualToString:@"outline"] && [[attributeDict valueForKey:@"text"] isEqualToString:@"You may also like"])
{
flagCheck = 1;
}
else if ([elementName isEqualToString:@"outline"] && [[attributeDict valueForKey:@"text"] isEqualToString:@"Genres"])
{
flagCheck = 0;
}
if (flagCheck == 1 && [elementName isEqualToString:@"outline"])
{
if([[attributeDict valueForKey:@"type"] isEqualToString:@"audio"])
{
[appDelegate.objPlayer.recommendDataArray addObject:attributeDict];

}
}
}
else
{
if ([elementName isEqualToString:@"location"])
{
flagCheck = 2;
}
else if ([elementName isEqualToString:@"url"])
{
flagCheck = 3;
}
else if ([elementName isEqualToString:@"name"])
{
flagCheck = 4;
}
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (flagCheck == 2)
{
flagCheck = -1;
appDelegate.objPlayer.flickrCity = string;
}
else if(flagCheck == 3)
{
flagCheck = -1;
appDelegate.objPlayer.stationURL = string;
}
else if(flagCheck == 4)
{
flagCheck = -1;
appDelegate.playStationName = string;
}
//else if(flagCheck == 0) // change
// {
// appDelegate.playStationName = string;
// }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//if (flagCheck == 1 && [elementName isEqualToString:@"outline"])
// {
// [appDelegate.objPlayer.recommendDataArray addObject:dataDictionary];
// dataDictionary = nil;
// [dataDictionary release];
// }
}

最佳答案

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string可能会被多次调用,因此您需要将找到的字符累积到 NSMutableString 中。 Event-Driven XML Programming Guide 中有一个如何实现这一点的示例。 .

The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

现在 \u00e9é 的 UTF-16,因此必须正确编码数据才能解析过去的\u00。因此,如果您的数据最初是一个字符串,您可以像这样从中获取数据。

NSString *text = @"<node>Ch\u00e9rie</node>";
//Important or the parser will stop after Ch
NSData *utf16encode = [text dataUsingEncoding:NSUTF16StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:utf16encode];

关于iphone - 难以解析包含 '\u00' 等字符的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com