Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > XML root node attributes

Reply
Thread Tools

XML root node attributes

 
 
Slafs
Guest
Posts: n/a
 
      11-17-2009
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.
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      11-17-2009
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
 
Reply With Quote
 
 
 
 
Matt Mitchell
Guest
Posts: n/a
 
      11-17-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=(E-Mail Removed)
[mailtoython-list-bounces+mmitchell=(E-Mail Removed)] On
Behalf Of Slafs
Sent: Tuesday, November 17, 2009 9:20 AM
To: http://www.velocityreviews.com/forums/(E-Mail 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()
 
Reply With Quote
 
Slafs
Guest
Posts: n/a
 
      11-18-2009
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)


On 17 Lis, 15:36, Stefan Behnel <(E-Mail Removed)> wrote:
> 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


 
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
Getting to the root node of an xml document Rodolphe G Java 4 06-01-2007 07:49 PM
Problem updating an attribute that appears in both root node and child node! Tedros.G@googlemail.com XML 1 05-08-2007 11:32 AM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
Please help extract attributes from root node. RichardHatcher.com@gmail.com XML 2 06-08-2006 06:57 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM



Advertisments