XML root node attributes

S

Slafs

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.
 
S

Stefan Behnel

Slafs, 17.11.2009 15:19:
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.

Yes, you /can/, but you /should/ use 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>

Use ElementTree:

import xml.etree.ElementTree as ET
root = ET.Element("root", dict(a='v', b='v2', c='v3'))
root.SubElement('d')

print ET.tostring(root)

Stefan
 
M

Matt Mitchell

-----------------------------------
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: [email protected]
[mailto:p[email protected]] On
Behalf Of Slafs
Sent: Tuesday, November 17, 2009 9:20 AM
To: (e-mail address removed)
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()
 
S

Slafs

Thanks

But this doesn't work. I've ended using something like this:

import xml.etree.ElementTree as ET
root = ET.Element("root", dict(a='v', b='v2', c='v3'))
n = ET.SubElement(root,'d')
tree = ET.ElementTree(root)
import sys
tree.write(sys.stdout)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top