views:

112

answers:

2

Hi

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?

A: 

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

Joshua Weinberg
I tried this but it does not help. there is no exception thrown and i cannot catch it:<pre><code> @try { [parser parse]; } @catch (NSException * e) { NSLog(@"Parse error"); NSString * str = [[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] autorelease]; NSLog(str); }</code></pre>(sorry for the lack of formatting, cannot get it working with spaces in front of the code it seems)
Joris Mans
A: 

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.

Kalle