8.13.2 ElementTree Objects
-
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.
- 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.
-
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.
-
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.
-
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.
-
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.
-
Gets the root element for this tree.
- An element instance.
-
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.
- 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.