XML root node attributes

Matt Mitchell mmitchell at transparent.com
Tue Nov 17 15:40:36 CET 2009




 
-----------------------------------
The information contained in this electronic message and any attached document(s) is intended only for the personal and confidential use of the designated recipients named above. This message may be confidential. If the reader of this message is not the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone (603) 262-6300 or by electronic mail immediately. Thank you.
 
-----Original Message-----
From: python-list-bounces+mmitchell=transparent.com at python.org
[mailto:python-list-bounces+mmitchell=transparent.com at python.org] On
Behalf Of Slafs
Sent: Tuesday, November 17, 2009 9:20 AM
To: python-list at python.org
Subject: XML root node attributes

Hi

I'm little confused about adding attributes to the root node when
creating an XML document.
Can I do this using minidom or something else.
I can't find anything that would fit my needs.

i would like to have something like this:
<?xml ... ?>
<root a="v" b="v2" c="v3">
    <d ... > </d>
   ....
</root>

Please help.

Regards.
-- 
http://mail.python.org/mailman/listinfo/python-list


Hi,

I'm sure someone will point out a better way to do it but yes, you can
do it with minidom.

from xml.dom.minidom import Document

doc = Document()

root = doc.createElement('root')
root.setAttribute('a', 'v')
root.setAttribute('b', 'v2')
root.setAttribute('c', '3')
doc.appendChild(root)

d = doc.createElement('d')
root.appendChild(d)

print doc.toprettyxml()



More information about the Python-list mailing list