PyDocs

Nav

Structured Markup Processing Tools

ElementTree

  • ElementTree(element=None, file=None)
An XML element hierarchy.

This class also provides support for serialization to and from
standard XML.

*element* is an optional root element node,
*file* is an optional file handle or file name of an XML file whose
contents will be used to initialize the tree with.
Members of ElementTree

_

_setroot
Replace root element of this tree.

f

find
Find first matching element by tag name or path.
findall
Find all matching subelements by tag name or path.
findtext
Find first matching element by tag name or path.

g

getroot
Return root element of this tree.

i

iter
Create and return tree iterator for the root element.
iterfind
Find all matching subelements by tag name or path.

p

parse
Load external XML document into element tree.

w

write
Write element tree to a file as XML.
1 Example
>>> from xml.etree.ElementTree import ElementTree
>>> tree = ElementTree()
>>> tree.parse("index.xhtml")
<Element 'html' at 0xb77e6fac>
>>> p = tree.find("body/p")     # Finds first occurrence of tag p in body
>>> p
<Element 'p' at 0xb77ec26c>
>>> links = list(p.iter("a"))   # Returns list of all links
>>> links
[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
>>> for i in links:             # Iterates through all found links
...     i.attrib["target"] = "blank"
...
>>> tree.write("output.xhtml")
from the Python docs
See Also
No see-alsos for xml.etree.ElementTree/ElementTree
0 Notes
No notes for ElementTree