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")
0The 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).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access