0

我正在尝试使用é之类的字符解析 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

4

1 回答 1

3

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

解析器对象可以向委托发送几个 parser:foundCharacters: 消息来报告元素的字符。因为字符串可能只是当前元素的全部字符内容的一部分,所以您应该将它附加到当前的字符累积中,直到元素发生变化。

我假设您的代码不会累积字符,而只是使用上次 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

所以这不是编码问题。

于 2013-01-07T18:17:08.967 回答