开发者

NSXMLParser crashing on invalid XML

I have a parser working correctly when the input is in valid XML form. Is there any way for me to know when an XML document isn't correctly formatted and to recover from that without my application crashing?

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData]; [xmlParser setDelegate:self]; [xmlParser setShouldResolveExternalEntities:YES]; success = [xmlParser parse];

The application crashes when calling [xmlParser parse] and does not call my parseErrorOccurred delegate method.

EDIT

It has nothing to do with the parser my parseErrorOccurred delegate开发者_如何学Go method was causing the crash. An oversight with calling NSLog was causing the issue.


Well, the NSXMLParserDelegate has a method parser:parseErrorOccurred: which is supposedly called when a fatal error has occured, so you can catch it there and decide on how to proceed (all the info you have parsed thus far is available).

I assume the crash occurs, because the parser isn't returning what was expected of it, so if you catch the error happening and try to fix the data and/or replace something necessary then your app shouldn't crash anymore.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜