Minidom XML output - attributes in wrong order ?

Peter Møllerud cozmo at nospam.oslo.online.no
Tue Sep 5 05:31:37 EDT 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 





More information about the Python-list mailing list