Create an XML document

Stefan Behnel stefan.behnel-n05pAM at web.de
Tue May 22 20:30:53 EDT 2007


kyosohma 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



More information about the Python-list mailing list