473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2473
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: takarimasu | last post by:
How can i create an input object (text area,select) at runtime ? B.
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.