Simple XML-to-Python conversion

Fredrik Lundh fredrik at pythonware.com
Thu Mar 17 08:18:53 EST 2005


"gaudetteje at gmail.com" wrote:

> I've been searching high and low for a way to simply convert a small
> XML configuration file to Python data structures.  I came across gnosis
> XML tools, but need a built-in option for doing something similar.
>
> My knowledge of DOM and anything beyond simple XML structures is
> rudimentary at best.  Is there a simple way to use SAX handlers to pull
> attributes and elements into Python lists/dictionaries?

tools like ElementTree and xmltramp are very easy to use. ElementTree builds
a tree of Element objects which behave like lists (of subelements) and contain
dictionaries (of attributes).  xmltramp builds a tree where both subelements and
attributes are mapped to object attributes.

    http://www.aaronsw.com/2002/xmltramp/
    http://www.effbot.org/zone/element-index.htm

if you want to ship any of them with your application, you only need a single
module (xmltramp.py and ElementTree.py, respectively).

API summaries:

    http://reagle.org/joseph/blog/technology/python/elementtree-model
    http://reagle.org/joseph/blog/technology/python/xmltramp-model

(xmltramp might be a bit easier to use for simple cases, elementtree is a bit more
flexible and a bit more efficient, especially if you're using the C implementation)

</F> 






More information about the Python-list mailing list