1
votes

Anyone else experiencing crashes deep down in the iPhone libraries when NSXMLParser parses an xml containing errors? I thought it was supposed to call:

  • (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError

but instead it crashes the entire app somewhere inside _xmlRaiseError.

Is anyone else experiencing this and is there a way to catch this, instead of having my program crash?

5

5 Answers

1
votes

The error handling is not found in the TouchXML framework or in the CXMLDocument. It is in the libxml framework which will (to my knowledge) output a string but not raise an exception. So it is all about passing an error pointer and then reading it straight after. If it is not nil, an error has occurred. If you are getting crashes the error should be somewhere else... Hope that helps.

0
votes

You should be able to use @try/@catch to wrap this if you need to handle all kinds of malformed XML.

0
votes

The XML parser never crashes for me, but my handlers have crashed on occasion. For example if I get < foo /> and try to store the value of it in an array (nil, boom). The following is the exact code I use, which parses XML using a delegate that I've made.

NSData *data = [[NSData alloc] initWithContentsOfFile:filename];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
MGXMLParser *parser = [[MGXMLParser alloc] initWithRecipient:self];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
if (success) {
     NSLog(@"No errors");
} else {
    NSError *error = [xmlParser parserError];
    NSLog(@"Errors with xmlParser: %@", [error localizedDescription]);
}
[parser release];
[xmlParser release];
[data release];

MGXMLParser is my own class which is a delegate for the XML parser, in case it wasn't obvious.

Update: oops, SO parsed my < foo/ > into nothingness.

0
votes

The problem is probably that your XML string gets autoreleased before parseErrorOccurred ever gets called, causing a call to a dealloc'd object.

The solution is do to something like:

NSData *data = [[contentString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] retain]; //use a retain, to stop data being autoreleased

NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data];

[xmlParse setDelegate:self];

[xmlParse parse];

[data release]; //now release data
[xmlParse release];
0
votes

I filed this as a bug report and Apple answered me 1 year later to say it should be fixed in iOS5.