Python Glossary Русский

all Python 3xml.etree.ElementTreeexception

xml.etree.ElementTree.ParseError

exceptions

Raised when parsing malformed XML. A subclass of SyntaxError; it carries code and position — the line and column of the error.

Syntax

except ET.ParseError as err: err.position, err.code

Catch ET.ParseError rather than a bare SyntaxError: the latter also catches bad find('//x') paths and hides different problems under one handler. position is a (line, column) tuple. Malformed XML aborts the whole parse: fromstring() returns no partially parsed tree.

Examples

import xml.etree.ElementTree as ET
try:
    ET.fromstring('<a><b></a>')
except ET.ParseError as err:
    print(err)  # → mismatched tag: line 1, column 8
    print(err.position, isinstance(err, SyntaxError))  # → (1, 8) True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy