Next / Previous / Contents / TCC Help System / NM Tech homepage

7.5. The parse() function: build an ElementTree from a file

The quickest way to convert an XML file into an ElementTree is to use this function:

etree.parse(source)

where source is the name of the file, or a file object containing the XML. If the file is well-formed, the function returns an ElementTree instance.

Exceptions raised include:

IOError

The file is nonexistent or not readable.

etree.XMLSyntaxError

The file is readable, but does not contain well-formed XML. The returned exception contains an .error_log attribute that you can print to see where the error occurred. For an example of the display of the error_log, see Section 7.3, “The ElementTree() constructor”.