Create an XML document

Matimus mccredie at gmail.com
Tue May 22 14:30:40 EDT 2007


> How do I get Python to put values into the elements by tag name?

The same way you create put in new elements. Only, you use
'doc.createTextNode' instead of 'doc.createElement' to create it.


<code>

from xml.dom.minidom import Document

doc = Document()

zappt = doc.createElement('zAppointments')
zappt.setAttribute('reminder', '15')
doc.appendChild(zappt)

appt = doc.createElement('appointment')
zappt.appendChild(appt)

begin = doc.createElement('begin')
begincont = doc.createTextElement('1179775800')
begin.appendChild(begincont)
appt.appendChild(begin)

duration = doc.createElement('duration')
durationcont = doc.createTextElement('1800')
duration.appendChild(durationcont)
appt.appendChild(duration)

f = file(r'path\to\file.xml', 'w')
f.write(doc.toprettyxml(indent='    '))
f.close()

</code>




More information about the Python-list mailing list