Handling Parsing Errors

The parseError object contains the most recent errors encountered by the parser. For example, consider loading a document that does not exist:

>>> msxml.load("NotReally.xml")
0

The return result of 0 is enough to indicate that something has gone wrong. At this point, you can retrieve the parseError object to inspect for damage:

>>> pe = msxml.parseError

The parseError interface is detailed at the end of this appendix’s reference section. The object essentially encapsulates seven attributes of an XML parsing error and makes them available via the ParseError object:

>>> print pe.errorCode
-2146697211
>>> print pe.reason
The system cannot locate the resource specified.

The error conditions hint that it may be wise to also check the URL that the parser was using to load the document:

>>> print pe.url
NotReally.xml

As you can see, the problem is that the parser is after a file that does not exist. In addition to a reason and an errorCode, the parser also contains information concerning location within the file of a parsing error (filepos, line, and linepos).

Get Python & XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.