8.13.2 ElementTree Objects

class ElementTree( [element,] [file])
ElementTree wrapper class. This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML.

element is the root element. The tree is initialized with the contents of the XML file if given.

_setroot( element)
Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. element is an element instance.

find( path)
Finds the first toplevel element with given tag. Same as getroot().find(path). path is the element to look for.

The first matching element, or None if no element was found.

findall( path)
Finds all toplevel elements with the given tag. Same as getroot().findall(path). path is the element to look for.

A list or iterator containing all matching elements, in section order.

findtext( path[, default])
Finds the element text for the first toplevel element with given tag. Same as getroot().findtext(path). path is the toplevel element to look for. default is the value to return if the element was not found.

The text content of the first matching element, or the default value no element was found. Note that if the element has is found, but has no text content, this method returns an empty string.

getiterator( [tag])
Creates a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. tag is the tag to look for (default is to return all elements)

An iterator.

getroot( )
Gets the root element for this tree.

An element instance.

parse( source[, parser])
Loads an external XML section into this element tree. source is a file name or file object. parser is an optional parser instance. If not given, the standard XMLTreeBuilder parser is used.

The section root element.

write( file[, encoding])
Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding is the output encoding (default is US-ASCII).

See About this document... for information on suggesting changes.