简体   繁体   English

接收带有特殊字符的XML

[英]Receive XML with special characters

I'm trying to parse XML with characters like é , ñ I'm using UTF8 as encoding.. 我正在尝试使用诸如é这样的字符来解析XML 我正在使用UTF8作为编码。

I have already tried change the encode to NSISOLatin1StringEncoding but It doesn't work 我已经尝试将编码更改为NSISOLatin1StringEncoding,但是它不起作用

The code is: 代码是:

   -(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];
   }

When I receive the xml response...The results with special characters appears wrong... 当我收到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...

  }

Example: Caperuçú appears çú , Indianópolis appears ópolis 例如: Caperuçú出现çúIndianópolis出现ópolis

See the documentation of the parser:foundCharacters: delegate method: 请参阅parser:foundCharacters:的文档parser:foundCharacters:委托方法:

The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. 解析器对象可以向委托发送多个parser:foundCharacters:消息以报告元素的字符。 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. 因为字符串可能只是当前元素的全部字符内容的一部分,所以应将其附加到当前的字符累积中,直到元素更改为止。

I assume that your code does not accumulate the characters and just uses the result of the last parser:foundCharacters: call. 我假设您的代码不累积字符,而仅使用最后一个 parser:foundCharacters:调用的结果。

The following sample program shows this effect with your input strings: 下面的示例程序通过您的输入字符串显示了这种效果:

-(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);
}

Output: 输出:

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

So this is not an encoding issue. 因此,这不是编码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM