469,331 Members | 5,817 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Create an XML document

Hi all,

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>

I tried using minidom with the following code:

<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')
appt.appendChild(begin)
f = file(r'path\to\file.xml', 'w')
f.write(doc.toprettyxml(indent=' '))
f.close()

</code>
This gives me the following:

<?xml version="1.0" ?>
<zAppointments reminder="15">
<appointment>
<begin/>
<duration/>
</appointment>
</zAppointments>

How do I get Python to put values into the elements by tag name? I can
parse my documents just fine, but now I need to edit them and write
them out to a file for an application I am working on. I am sure I am
missing something obvious.

Thanks a lot!

Mike

May 22 '07 #1
5 2323
ky******@gmail.com skrev:
Hi all,

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>

I tried using minidom with the following code:

<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')
appt.appendChild(begin)
f = file(r'path\to\file.xml', 'w')
f.write(doc.toprettyxml(indent=' '))
f.close()

</code>
How do I get Python to put values into the elements by tag name? I can
parse my documents just fine, but now I need to edit them and write
them out to a file for an application I am working on. I am sure I am
missing something obvious.
From http://wiki.python.org/moin/MiniDom

Add an Element with Text Inside

Create & add an XML element to an XML document, the element has text inside.

ex: <foo>hello, world!</foo>
from xml.dom.minidom import parse

dom = parse("bar.xml")
x = dom.createElement("foo") # creates <foo />
txt = dom.createTextNode("hello, world!") # creates "hello, world!"
x.appendChild(txt) # results in <foo>hello, world!</foo>
dom.childNodes[1].appendChild(x) # appends at end of 1st child's \ children
print dom.toxml()
May 22 '07 #2
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>

May 22 '07 #3
On May 22, 10:00 am, kyoso...@gmail.com wrote:
Hi all,

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>

I tried using minidom with the following code:

<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')
appt.appendChild(begin)
f = file(r'path\to\file.xml', 'w')
f.write(doc.toprettyxml(indent=' '))
f.close()

</code>

This gives me the following:

<?xml version="1.0" ?>
<zAppointments reminder="15">
<appointment>
<begin/>
<duration/>
</appointment>
</zAppointments>

How do I get Python to put values into the elements by tag name? I can
parse my documents just fine, but now I need to edit them and write
them out to a file for an application I am working on. I am sure I am
missing something obvious.

Thanks a lot!

Mike
Thanks Nis. Your code worked great. For some reason, Google Groups
seems hosed up today and makes seeing replies impossible on some
posts.

Maximus - What the? You told me to use "createTextNode" and then you
used "doc.createTextElement". I think I know what you mean though.

Thank you both for your advice. It works now.

Mike

May 22 '07 #4
ky******@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
May 23 '07 #5
On May 22, 7:30 pm, Stefan Behnel <stefan.behnel-n05...@web.dewrote:
kyoso...@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

May 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.