I'm trying to parse XML with characters like é, ñ I'm using UTF8 as encoding..
I have already tried change the encode to NSISOLatin1StringEncoding but It doesn't work
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...
- (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
See the documentation of the parser:foundCharacters:
delegate method:
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.
I assume that your code does not accumulate the characters and just uses the result of the last parser:foundCharacters:
call.
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.