gpt4 book ai didi

objective-c - 接收带有特殊字符的 XML

转载 作者:行者123 更新时间:2023-11-28 20:25:22 25 4
gpt4 key购买 nike

我正在尝试使用 éñ 等字符解析 XML 我正在使用 UTF8 作为编码..

我已经尝试将编码更改为 NSISOLatin1StringEncoding 但它不起作用

代码是:

   -(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
// NSLog(theXML);
[theXML release];
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
if (connection) {
[connection release];
}
}


-(void)callWS {
NSString *url = @"theUrlHere";
NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}

当我收到 xml 响应时...带有特殊字符的结果显示错误...

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if(!soapResults)
{
....//the soapResults here appears wrong when It has special chars...

}

示例:Caperuçú 出现在 çúIndianópolis 出现在 ópolis

最佳答案

请参阅parser:foundCharacters: 委托(delegate)方法的文档:

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.

我假设您的代码不会累积字符,而只是使用 last parser:foundCharacters: 调用的结果。

以下示例程序使用您的输入字符串显示了此效果:

-(void)parse
{
NSString *xmlString = @"<a><b>Indianópolis</b><c>Caperuçú</c></a>";
NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
parser.delegate = self;
[parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSLog(@"didStartElement: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"didEndElement: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"foundCharacters: %@", string);
}

输出:

didStartElement: a
didStartElement: b
foundCharacters: Indian
foundCharacters: ópolis
didEndElement: b
didStartElement: c
foundCharacters: Caperu
foundCharacters: çú
didEndElement: c
didEndElement: a

所以这不是编码问题。

关于objective-c - 接收带有特殊字符的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201136/

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