Forums » Saxon/C Help and Discussions »
Should PyDocumentBuilder have exception_occurred/error_message properties to report parse errors?
Added by Martin Honnen almost 2 years ago
I am wondering how to handle errors occurring during XML parsing with PyDocumentBuilder in my code, it seems the document builder itself has no properties like exception_occurred or error_message, those on the PySaxonProcessor don't seem to be set if a parse error occurs, only the standard err output gets an error message, at least as far as I can tell from the following code:
from saxonche import *
with PySaxonProcessor(license=True) as proc:
print(proc.version)
proc.exception_clear()
doc_builder = proc.new_document_builder()
xdm_node = doc_builder.parse_xml(xml_text='<root><foo></root>')
if proc.exception_occurred:
print('Error parsing XML: ' + proc.error_message)
proc.exception_clear()
exit(1)
else:
print(xdm_node)
Output:
SaxonC-HE 12.0 from Saxonica
Error on line 1 column 14
SXXP0003 Error reported by XML parser: The element type "foo" must be terminated by the
matching end-tag "</foo>".: The element type "foo" must be terminated by the matching
end-tag "</foo>".
Process finished with exit code -1073741819 (0xC0000005)
Shouldn't my Python code be able to handle/read out the parse error other than it being written to standard err by the C++ code?
Replies (3)
Please register to reply
RE: Should PyDocumentBuilder have exception_occurred/error_message properties to report parse errors? - Added by O'Neil Delpratt almost 2 years ago
The exception handling methods are available in the C++ code, missing from the Python PyDocumentBuilder class. I will file a bug issue for this.
RE: Should PyDocumentBuilder have exception_occurred/error_message properties to report parse errors? - Added by O'Neil Delpratt almost 2 years ago
Bug issue created here: #5854
RE: Should PyDocumentBuilder have exception_occurred/error_message properties to report parse errors? - Added by O'Neil Delpratt almost 2 years ago
The issue with the the example code is the PySaxonProcessor needs proc.set_cwd('file://example')
. This workaround still does not look correct, but for a well-formed XML text it will work. I have patched the problems for this bug.
Please register to reply