Python object <-> XML

Stefan Behnel stefan.behnel-n05pAM at web.de
Tue Sep 4 08:20:20 CEST 2007


Samuel wrote:
> Say you have the following XML:
> 
> <item ref="1">
>   <name>item 1</name>
> </item>
> <item ref="2">
>   <name>item 2</name>
> </item>
> <group>
>   <item ref="1" />
>   <item ref="2" />
>   <name>my group</name>
> </group>
> 
> Is there an easy way (i.e. without writing a sax/dom parser) to load
> this into a (number of) Python object(s), manipulate the instance, and
> save the result back to XML?

Try lxml.objectify. It doesn't copy your data into other objects, but it gives
you all the freedom to design your own objects as an abstraction of the XML
data. See here:

http://codespeak.net/lxml/objectify.html

especially these sections:

http://codespeak.net/lxml/objectify.html#element-access-through-object-attributes
http://codespeak.net/lxml/objectify.html#python-data-types
http://codespeak.net/lxml/objectify.html#defining-additional-data-classes

It's part of lxml, which makes it plenty fast, highly flexible and gives you
all the XML features you might ever need. :)

Stefan



More information about the Python-list mailing list