Is there a way I can preserve the original order of attributes when processing XML with minidom?
Say I have: <color red="255" green="255" blue="233" />
when I modify this with minidom the attributes are rearranged alphabetically blue, green, and red. I'd like to preserve the original order.
I am processing the file by looping through the elements returned by elements = doc.getElementsByTagName('color') and then I do assignments like this e.attributes["red"].value = "233".
1.Custom your own 'Element.writexml' method.
from 'minidom.py' copy Element's writexml code to your own file.
rename it to writexml_nosort,
delete 'a_names.sort()' (python 2.7)
or change 'a_names = sorted(attrs.keys())' to 'a_names = attrs.keys()'(python 3.4)
change the Element's method to your own:
minidom.Element.writexml = writexml_nosort;
2.custom your favorite order:
right_order = ['a', 'b', 'c', 'a1', 'b1']
3.adjust your element 's _attrs
node._attrs = OrderedDict( [(k,node._attrs[k]) for k in right_order ]  )
Preserve order of attributes when modifying with minidom