NSXMLParser parser:parseErrorOccurred: not called

2019-07-23 17:06发布

问题:

I have a very strange issue on iOS7.

I use standard NSXMLParser. On iOS6 everything works just fine - when XML is valid as well as when NSData is zero bytes or contains invalid bytes.

self.dataParser = [[[NSXMLParser alloc] initWithData:data] autorelease];
[self.dataParser setDelegate: self];
[self.dataParser parse];

On iOS7 parser works as expected only when it has a valid XML data. If data is zero bytes, none of the delegate methods are called.

Please help me find a direction to look to. Thanks.

UPD. All delegate methods are implemented (they work well on iOS6). I could check if data contains non-zero bytes (and data == nil), but I also must handle non-valid XML situations...

回答1:

It appears that on iOS 7 parser:parseErrorOccurred: delegate method is not being called when data is zero bytes or is not valid in UTF8 encoding.

It became a revelation to me that I don't have to use parserDidEndDocument: and parser:parseErrorOccurred: methods to detect finish of parsing, I can just check return value of parse method!

It's like I've been blind all these years :)

Big thanks to Martin R.



回答2:

You should always check if your data is OK. You should implement delegate method:

parser:parseErrorOccurred

and check what was wrong and handle an error.

In your case you only should check if(!data).