Create an XML document

kyosohma at gmail.com kyosohma at gmail.com
Wed May 23 08:57:35 EDT 2007


On May 22, 7:30 pm, Stefan Behnel <stefan.behnel-n05... at web.de> wrote:
> kyoso... at gmail.com wrote:
> > I am attempting to create an XML document dynamically with Python. It
> > needs the following format:
>
> > <zAppointments reminder="15">
> >    <appointment>
> >            <begin>1179775800</begin>
> >            <duration>1800</duration>
> >         </appointment>
> > </zAppointments>
>
> Try lxml.objectify.
>
> http://codespeak.net/lxml/dev/objectify.html
>
>   >>> from lxml import etree, objectify
>   >>> zAppointments = objectify.Element("zAppointments")
>   >>> zAppointments.set("reminder", "15")
>   >>> zAppointments.appointment = objectify.Element("appointment")
>   >>> zAppointments.appointment.begin = 1179775800
>   >>> zAppointments.appointment.duration = 1800
>
>   >>> print etree.tostring(zAppointments, pretty_print=True)
>   <zAppointments reminder="15">
>     <appointment>
>       <begin>1179775800</begin>
>       <duration>1800</duration>
>     </appointment>
>   </zAppointments>
>
> Pretty much what one would expect.
>
> Stefan

Stefan,

This looks really cool. I'll try implementing it sometime today and
see if it affects my execution time any. It's definitely clearer
code...at least in my opinion.

Mike




More information about the Python-list mailing list