
“But I have found that sitting under the ElementTree, one can feel the Zen of XML.”
— Essien Ita Essien
Update 2007-09-12:ElementTree 1.3 alpha 3 is now available. For more information, seeIntroducing ElementTree 1.3.
Update 2007-08-27:ElementTree 1.2.7 preview is now available. This is 1.2.6 plus support for IronPython. The serializer is ~20% faster, and now supports newlines in attribute values.
TheElement type is a simple but flexible container object,designed to store hierarchical data structures, such as simplified XMLinfosets, in memory. The element type can be described as a crossbetween a Python list and a Python dictionary.
TheElementTree wrapper type adds code to load XML files as treesof Element objects, and save them back again.
TheElement type is available as a pure-Python implementationfor Python 1.5.2 and later. AC implementationis also available, for use with CPython 2.1 and later. The corecomponents of both libraries are also shipped with Python 2.5 andlater.
There’s also an independent implementation,lxml.etree (dead link), based on the well-known libxml2/libxslt libraries. This adds full support for XSLT, XPath, andmore.
For more implementations and add-ons, see theInteresting Stuffsection below.
[usage][documentation][api reference]
Binary installers are available for many platforms, includingWindows, Mac OS X, and most Linux distributions. Look for packagesnamed “python-elementtree” or similar.
To install from source, simply unpack the distribution archive,change to the distribution directory, and run thesetup.py scriptas follows:
$ python setup.py install
When you’ve done this, you should be able to import theElementTreemodule, and other modules from theelementtree package:
$ python>>>from elementtreeimport ElementTree
It’s common practice to import ElementTree under an alias, both tominimize typing, and to make it easier to switch between differentimplementations:
$ python>>>import elementtree.ElementTreeas ET>>>import cElementTreeas ET>>>import lxml.etreeas ET>>>import xml.etree.ElementTreeas ET# Python 2.5
Note that if you only need the core functionality, you can include theElementTree.py file in your own project. To getpath support, you also needElementPath.py. All other modules are optional.
EachElement instance can have an identifying tag, any numberof attributes, any number of child element instances, and an associatedobject (usually a string). To create elements, you can use theElement orSubelement factories:
import elementtree.ElementTreeas ET# build a tree structureroot = ET.Element("html")head = ET.SubElement(root,"head")title = ET.SubElement(head,"title")title.text ="Page Title"body = ET.SubElement(root,"body")body.set("bgcolor","#ffffff")body.text ="Hello, World!"# wrap it in an ElementTree instance, and save as XMLtree = ET.ElementTree(root)tree.write("page.xhtml")
TheElementTree wrapper adds code to load XML files as treesof Element objects, and save them back again. You can use theparsefunction to quickly load an entire XML document into an ElementTreeinstance:
import elementtree.ElementTreeas ETtree = ET.parse("page.xhtml")# the tree root is the toplevel html elementprint tree.findtext("head/title")# if you need the root element, use getrootroot = tree.getroot()# ...manipulate tree...tree.write("out.xml")
For more details, seeElements and Element Trees.
Zone articles:
Elsewhere:
Andrew Dalke:IterParseFilter: XPath-like filtering of ElementTree’s iterparse event stream
Andrew Dalke:PyProtocols for output generation
Martijn Faassen:lxml and (c)ElementTree
Andrew Kuchling:Processing XML with ElementTree [slides from a talk]
Danny Yoo:ElementTree mini-tutorial [“Let’s work through a small example with it; that may help to clear some confusion.“]
Joseph Reagle:XML ElementTree Data Model
Uche Ogbuji:Simple XML Processing With elementtree [xml.com]
David Mertz:Process XML in Python with ElementTree: How does the API stack up against similar libraries? [ibm developerworks]
Uche Ogbuji:
Python Paradigms for XML(dead link)Uche Ogbuji:XML Namespaces Support in Python Tools, Part Three [xml.com]
Uche Ogbuji:Practical SAX Notes: ElementTree, Namespaces and Techniques for Large Documents [xml.com]
Interesting stuff built with (or for) ElementTree (selection):
L. C. Rees:webstring(webstring is a web templating engine that allows programs to manipulateXML and HTML documents with standard Python sequence and stringoperators. It is designed for those whose preferred web templatelanguages are Python and HTML (and XML for people who swing that way).
Chris McDonough:meld3 (an XML templating system for Python 2.3+ which keeps template markup and dynamic rendering logic separate from one another, based on PyMeld)
Peter Hunt:pymeld4 (another ET-based implementation of the PyMeld templating language)
Seo Sanghyeon:pyexpat/ElementTree for IronPython (a pyexpat emulation for IronPython which lets you use the standard ElementTree module on that platform)
Oren Tirosh:
ElementBuilder(dead link)(friendly syntax for constructing ElementTree:s)Staffan Malmgren:lagen.nu (anicely formatted, hyperlinked, linkable, and taggable version ofthe entire body of swedish law) (
moreinformation(dead link))Ralf Schlatterbeck:OOoPy (a tool to inspect, create, and modify OpenOffice.org documents in Python)
Martijn Faassen:
lxml(dead link)(ElementTree-compatible bindings forlibxml2andlibxslt).Martin Pool, et al:Bazaar-NG (version management system)
Seth Vidal, Konstantin Ryabitsev, et al:Yellow dog Updater, Modified (an automatic updater and package installer/remover for rpm systems)
Michael Droettboom:
pyScore(dead link) (a set of Python-based tools for working with symbolic music notation)Ryan Tomayko:Kid (a template language)
Ken Rimey:
PDIS XPath(dead link) (a more complete XPath implementation)Roland Leuthe:
minixsv(dead link) (a lightweight XML schema validator written in pure Python)Bruno da Silva de Oliveira, Joel de Guzman:Pyste (a Python binding generator for C++)
Works in progress:
- ElementTree: Working with Qualified Names
- Using the ElementTree Module to Generate Google Requests
- A Simple Technorati Client
- Using Element Trees to Parse WSDL Files
- Using Element Trees to Parse XBEL Files
- Using ElementTrees to Generate XML-RPC Messages
- Generating Tkinter User Interfaces from XML
- A Simple XML-Over-HTTP Class
- You Can Never Have Too Many Stock Tickers!
rendered by adjango application. hosted bywebfaction.