Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Minidom XML output - attributes in wrong order ?

Reply
Thread Tools

Minidom XML output - attributes in wrong order ?

 
 
Peter Møllerud
Guest
Posts: n/a
 
      09-05-2006
I'm very new to Python, so consider this a silly newbie question...
Anyway, I'm building a small application to generate a XML. Using document
from minidom, I'm doing something like :

from xml.dom.minidom import Document

doc = Document()

c = doc.createElement("sometest")
doc.appendChild(c)
tmp = doc.createElement("info")
tmp.setAttribute("vehicle", "car")
tmp.setAttribute("x-ray ", "100-1")
tmp.setAttribute("age", "30")
c.appendChild(tmp)
print doc.toprettyxml(indent=" ")

What it then prints out is :

<?xml version="1.0" ?>
<sometest>
<info age="30" vehicle="car" x-ray ="100-1"/>
</sometest>

What I expected was : <info vehicle="car" x-ray="100-1" age="30"/>

So it seems the dom module sorts the attributes alphabetically. Is there any
way I can prevent it from doing that ? What I want is to list them out in
the same order as they are added in the code...

Thanks,

Peter


 
Reply With Quote
 
 
 
 
Maxim Sloyko
Guest
Posts: n/a
 
      09-05-2006

> So it seems the dom module sorts the attributes alphabetically. Is there any
> way I can prevent it from doing that ? What I want is to list them out in
> the same order as they are added in the code...


I don't know how to do what you ask, I'm just here to warn you that you
shouldn't rely on the order of attributes in the document in any way,
because this is implementation dependent.

If you need to preserve order of some items -- use child elements
instead of attributes.

--
Regards, Maxim Sloyko

 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      09-09-2006
Peter Møllerud wrote:
> I'm very new to Python


then you might want to consider using ElementTree or lxml, not necessarily
minidom.


> c = doc.createElement("sometest")
> doc.appendChild(c)
> tmp = doc.createElement("info")
> tmp.setAttribute("vehicle", "car")
> tmp.setAttribute("x-ray ", "100-1")
> tmp.setAttribute("age", "30")
> c.appendChild(tmp)
> print doc.toprettyxml(indent=" ")
>
> What it then prints out is :
>
> <?xml version="1.0" ?>
> <sometest>
> <info age="30" vehicle="car" x-ray ="100-1"/>
> </sometest>
>
> What I expected was : <info vehicle="car" x-ray="100-1" age="30"/>


Attributes in XML are not ordered and no XML library will keep the order. All
you could do is serialise by hand, which is not that difficult either. Is
there any reason why you might want to keep the order?

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
xml.dom.minidom losing the XML document type attribute Johannes Bauer Python 7 06-11-2009 10:21 PM
Replacing _xmlplus.dom.minidom with xml.dom.minidom aine_canby@yahoo.com Python 3 08-03-2007 03:50 PM
Minidom output of XML escaped characters Derek Basch Python 2 03-25-2005 05:25 PM
Clarification on XML parsing & namespaces (xml.dom.minidom) Greg Wogan-Browne Python 1 01-28-2005 03:19 AM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM



Advertisments