Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Create an XML document

Reply
Thread Tools

Create an XML document

 
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      05-22-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Nis_J=F8rgensen?=
Guest
Posts: n/a
 
      05-22-2007
http://www.velocityreviews.com/forums/(E-Mail 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()
 
Reply With Quote
 
 
 
 
Matimus
Guest
Posts: n/a
 
      05-22-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>

 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      05-22-2007
On May 22, 10:00 am, (E-Mail Removed) 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

 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      05-23-2007
(E-Mail Removed) 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
 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      05-23-2007
On May 22, 7:30 pm, Stefan Behnel <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Api to create document and add,edit,delete text and t Krithika San Ruby 3 11-14-2009 09:29 PM
Using document.load() to get html NOT xml document icogs Javascript 0 01-31-2008 04:02 AM
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Refer to an XML document from within an XML document Manish Hatwalne XML 1 07-13-2004 10:24 AM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM



Advertisments