I am trying to use NSXMLParser to parse an XML file that looks something like this:
<?xml version="1.0" encoding="us-ascii"?>
<teams>
<team id = "A1">
<player1>John</player1>
<player2>José</player2>
</team>
...
</teams>
I use the following code:
NSString *urlString = [NSString stringWithFormat:@"http://www....abc.php?category=%@&poule=%c", @"S", 'B']; // Obviously, this contains an actual web address
NSURL *url = [NSURL URLWithString:urlString];
NSData *xml = [[NSData alloc] initWithContentsOfURL:url]; // <==
NSXMLParser *xmlParserObject = [[NSXMLParser alloc]initWithData:xml];
[xmlParserObject setDelegate:self];
[xmlParserObject parse];
and I implemented the didStartElement, foundCharacters, didEndElement and the parserErrorOccurred delegate functions.
This all goes well until a 'special' character, such as an é is encountered. The delegate method parserErrorOccurred reports the following error:
parser error: Error Domain=NSXMLParserErrorDomain Code=1544 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 1544.)"
parser error: Error Domain=NSXMLParserErrorDomain Code=5 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 5.)"
Then I replaced the part marked with '<==' with the following:
NSError *error;
NSData *xml = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (xml == nil) {
NSLog(@"*** Fatal error: %@\nuserInfo:%@", error, [error userInfo]);
}
and got the following error in addition to the one above:
*** Fatal error: Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x8158d90 {NSURL=http://www....abc.php?category=S&poule=B, NSStringEncoding=4}
userInfo:{
NSStringEncoding = 4;
NSURL = "http://www....abc.php?category=S&poule=B";
}
I also tried replacing the NSUTF8StringEncoding with any of the other encoders, such as NSISOLatin1StringEncoding, NSUTF16StringEncoding, NSASCIIStringEncoding, NSUnicodeStringEncoding and more. This resulted in the following error:
-[__NSCFString bytes]: unrecognized selector sent to instance 0x6e4cbc0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x6e4cbc0'
*** First throw call stack:
(0x12d0022 0x1781cd6 0x12d1cbd 0x1236ed0 0x1236cb2 0xce5f51 0xb447 0xaa89 0x1f2e330 0x1f2f439 0x908b9b24 0x908bb6fe)
terminate called throwing an exception(lldb)
I have no control over the contents of the XML, but if it indeed contains incorrect information, then maybe I can talk to the webmaster.
I'm fine with displaying the é character as 'e' or '?' if that's what it takes.
Any advice on what causes this error and how to correct or bypass it is greatly appreciated.
Tx!
--GB