xml config file to python object

Martin v. Loewis martin at v.loewis.de
Tue Mar 26 18:47:36 EST 2002


Thomas Guettler <pan-newsreader at thomas-guettler.de> writes:

> I would like to parse
> <myconfig>
>  <foo>2</foo>
>  <bar>sun</bar>
>  <bar>moon</bar>
> </myconfig>
> 
> to this:
> obj={'myconfig': [
>       { 'foo' : '2' }
>       { 'bar' : ['sun', 'moon']}
>       ]
>      }
> 
> I did it this weekend with a sax parser.

If this is the precise structure required, a SAX parser is probably as
good as any other approach. Most automatic toolkits would either build

obj={'myconfig': [
      { 'foo' : ['2'] }
      { 'bar' : ['sun', 'moon']}
      ]
     }

or

obj={'myconfig': [
      { 'foo' : '2' }
      { 'bar' : 'moon'}
      ]
     }

> Is there a way to pickle (serialize) any python object
> to xml and back?

If you are interested in "basic" objects, the xmlrpc module of Python
2.2 will already marshal them in a cross-language way. Likewise, the
PyXML xml.marshal package supports both WDDX support, and a
customizable marshaller.

For full pickling support, you can chose between xml_pickle, from

http://www-106.ibm.com/developerworks/xml/library/x-matters11.html

and the Zope xml pickle format ("ppml").

Regards,
Martin



More information about the Python-list mailing list