PIXEL
DOCK

I like the smell of Swift in the morning…

NSXMLParserErrorDomain error 5

Posted: | Author: | Filed under: iOS | Tags: , | No Comments »

Trying to parse an XML file I came across this error message. According to the error message the parser could not parse the XML file so I suspected that my XML was malformed. But it wasn’t.

It turned out the problem was that I used the wrong method to load the XML file.

Instead of using:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demo.xml" ofType:nil];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];

I tried to do this:

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"demo.xml"]];

So the problem was that the Parser could not really load the XML. Strangely enough the parser loaded something because otherwise the parser’s init method should have returned nil. But whatever the parser loaded, it surely was not the right stuff. So be sure to get the XML file’s path from the main bundle and feed that to the parser’s init method.