Create an XML document

K

kyosohma

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
 
?

=?ISO-8859-1?Q?Nis_J=F8rgensen?=

(e-mail address removed) 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()
 
M

Matimus

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>
 
K

kyosohma

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
 
S

Stefan Behnel

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
<zAppointments reminder="15">
<appointment>
<begin>1179775800</begin>
<duration>1800</duration>
</appointment>
</zAppointments>

Pretty much what one would expect.

Stefan
 
K

kyosohma

Try lxml.objectify.

http://codespeak.net/lxml/dev/objectify.html

<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top