Minidom XML output - attributes in wrong order ?

Stefan Behnel stefan.behnel-n05pAM at web.de
Sat Sep 9 03:05:15 EDT 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



More information about the Python-list mailing list