NSXML Parser always calls the "parseErrorOccurred" delegate first

141 views Asked by At

I have the following code,

NSString *string = "Some value";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
self.parser = [[NSXMLParser alloc] initWithData:data];
self.parser.delegate = self;
[self.parser parse];

After I call "parse" method it immediately calls the delegate parseErrorOccurred and then calls delegate parserDidEndDocument.

I want only one method to be called.

Thanks in advance.

2

There are 2 answers

0
Droppy On

That delegate method is called when there is a fatal error, so there is an issue with the XML you are parsing.

The method has an NSError parameter, and that should give you a clue as to what is wrong with the XML.

0
Zhu Shengqi On

You mean when an error occurs in parsing, you want only parseErrorOccurred() called and parserDidEndDocument() not called?

One possible solution: write parser.abortParsing() in parseErrorEccurred(). I'm not sure this would actually work, if it fails, try the solution below:

Use a flag in your class that wrap the parser. Set the flag to true in parseErrorEccurred(), then check this flag in parserDidEndDocument() (if it's false then execute other code).

Hope this will solve your problem. :)