[XML-SIG] how to output xml from a python object

Thomas B. Passin tpassin@comcast.net
Sat, 11 May 2002 10:23:31 -0400


[Dave Primmer]

> I'd like to do something like take a simple python object and make an xml
> file. I've looked and looked but I can't find any docs on how to do this
> (only parsing). http://ibm.com/developerworks/library/python2/ has some
old
> code that refers to modules that aren't available anymore but that's
> basically what i want to do. I don't want xmlPickle or generic
marshalling.
> My data format is relatively static and i'd like non-verbose XML. I'm sure
> it's in the archives but I can't download a 22 meg file! (death to
> pipermail)
>
> If there is no xml.dom.builder anymore, how do i build a dom tree and then
> write it out with .toxml()?
>
> I started out by just doing "<"+tag+">" stuff and writing elements for
> everthing but i'd like to be able to do xml attributes also.
>
> i'd like to do:
> object.name = 'bob'
> object.city = 'sea'
> object.desc = '<p>blabedy blah. on and on</p>'
> to this:
> <object "name="bob" "city"="sea"><desc><p>blabedy blah. on and
> on</p></desc></object>

If there isn't anything that does what you want, then use SAX.  If you can
work out how to traverse all the objects you are interested in, just throw
SAX events during the traverse.  You can modify a handler to build the
structures you want, then write it out.  The pickle module already knows how
to serialize objects, so it should be helpful to look at it.

Cheers,

Tom P