NSXMLParserErrorDomain 111

2020-04-07 07:03发布

The code below is printing the following message: Error Domain=NSXMLParserErrorDomain Code=111 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.)

mainUrl = [NSURL URLWithString:@"http://www.carris.pt/pt/carreiras"];
NSString *urlContents = [NSString stringWithContentsOfURL:mainUrl encoding:NSISOLatin1StringEncoding error:nil];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[urlContents dataUsingEncoding:NSISOLatin1StringEncoding]];
[xmlParser parse];
NSLog(@"%@", [xmlParser parserError]);

Anybody have a clue? As you can see by the code, the html is with ISO-8859-1 encoding.

Update: I submitted the url to the html validator site: http://validator.w3.org/ and it found over 30 errors. I think that has something to do with the error. But I can parse the html just fine with HPPLE.

4条回答
淡お忘
2楼-- · 2020-04-07 07:06

If you look in NSXMLParser.h, you'll see the list of error codes:

NSXMLParserInternalError = 1,
NSXMLParserOutOfMemoryError = 2,
NSXMLParserDocumentStartError = 3,
NSXMLParserEmptyDocumentError = 4,
NSXMLParserPrematureDocumentEndError = 5,
NSXMLParserInvalidHexCharacterRefError = 6,
NSXMLParserInvalidDecimalCharacterRefError = 7,
NSXMLParserInvalidCharacterRefError = 8,
NSXMLParserInvalidCharacterError = 9,
NSXMLParserCharacterRefAtEOFError = 10,
NSXMLParserCharacterRefInPrologError = 11,
NSXMLParserCharacterRefInEpilogError = 12,
...

So it looks like it's an NSXMLParserCharacterRefInPrologError, which is defined in the "Constants" section in the documentation. It says:

NSXMLParserCharacterRefInPrologError Invalid character found in the prolog. Available in iOS 2.0 and later. Declared in NSXMLParser.h.

查看更多
淡お忘
3楼-- · 2020-04-07 07:21

I ran into something similar while parsing XML. The problem maybe the encoding that you are using. Try UTF8 enconding instead of iOSLatin i.e.

NSString *urlContents = [NSString stringWithContentsOfURL:mainUrl encoding:NSUTF8StringEncoding error:nil];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[urlContents dataUsingEncoding:NSUTF8StringEncoding]];
查看更多
在下西门庆
4楼-- · 2020-04-07 07:28

Seems nobody has stumbled on the correct answer yet, so here it is.

In NSXMLParserError docs, it says:

The following error codes are defined by NSXMLParser. For error codes not listed here, see the <libxml/xmlerror.h> header file.

The number 111 isn't mentioned in this list, so we go to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/libxml/xmlerror.h, and find the value:

XML_ERR_USER_STOP, /* 111 */

There isn't a lot of documentation on XML_ERR_USER_STOP in libxml2, but from reading the changeset, it looks like it's a fast-fail when the parser sees an unexpected EOF.

查看更多
孤傲高冷的网名
5楼-- · 2020-04-07 07:28

I also had the "111" (undocumented?) error code when parsing. It turns out that the XML Parser expects the XML to be well formed (e.g., one root node that contains all the others).

查看更多
登录 后发表回答