Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Process XML in Python with ElementTree
By David Mertz, Ph.D. - 2003-12-04 Page:  1 2 3 4 5


ElementTree is a nice effort to bring a much lighter weight object model to XML processing in Python than that provided by DOM. Although I have not addressed it in this article, ElementTree is as good at generating XML documents from scratch as it is at manipulating existing XML data.

As author of a similar library, gnosis.xml.objectify, I cannot be entirely objective in evaluating ElementTree; nonetheless, I continue to find my own approach somewhat more natural in Python programs than that provided by ElementTree. The latter still usually utilizes node methods to manipulate data structures, rather than directly accessing node attributes as one usually does with data structures built within an application.

However, in several areas, ElementTree shines. It is far easier to access deeply nested elements using XPath than with manual recursive searches. Obviously, DOM also gives you XPath, but at the cost of a far heavier and less uniform API. All the Element nodes of ElementTree act in a consistent manner, unlike DOM's panoply of node types.


  • Participate in the discussion forum on this article. (You can also click Discuss at the top or bottom of the article to access the forum.)

  • Find out more about ElementTree at Fredrik Lundh's Element Trees page.

  • Get an additional perspective on the topic with this article by developerWorks columnist Uche Ogbuji.

  • Read David Mertz's earlier columns on XML libraries:
    • XML Matters #2 introduced gnosis.xml.objectify, then called simply xml_objectify (developerWorks, August 2000).
    • XML Matters #11 updated readers on some early improvements to gnosis.xml.objectify. Some newer features were not covered in this column, but are in the module's HISTORY and other documentation files (June 2001).
    • XML Matters #14 discussed the HaXml module for the Haskell lazy pure-functional programming language (October 2001).
    • XML Matters #18 discussed Ruby's REXML library (March 2002).
    You'll find all previous installments of David's XML Matters column at the column summary page.
  • Take a look at another Python XML API/library -- generateDS. Developer Dave Kuhlman has written a very nice essay comparing generateDS with gnosis.xml.objectify. In brief, the idea behind generateDS is to use an XML Schema as the basis for Python classes that properly handle the elements in an XML instance. Rather than handle XML trees generically, generateDS is code generator for Python modules to handle specific XML document schemas; autogenerated code can easily be specialized to quickly form a custom application.

  • Find more XML resources on the developerWorks XML zone.

  • Try IBM WebSphere Studio, a suite of tools that automate XML development, both in Java and in other languages. It is closely integrated with the WebSphere Application Server, but can also be used with other J2EE servers.

  • Find out how you can become an IBM Certified Developer in XML and related technologies.

View Process XML in Python with ElementTree Discussion

Page:  1 2 3 4 5 Next Page: Introduction

First published by IBM developerWorks

Copyright 2004-2019 All rights reserved.
Article copyright and all rights retained by the author.