Minidom XML output - attributes in wrong order ?

Peter Møllerud cozmo at
Tue Sep 5 11:31:37 CEST 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")
tmp = doc.createElement("info")
tmp.setAttribute("vehicle", "car")
tmp.setAttribute("x-ray ", "100-1")
tmp.setAttribute("age", "30")
print doc.toprettyxml(indent="  ")

What it then prints out is :

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

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



More information about the Python-list mailing list