Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML attributes get sorted

When I create a document using the minidom, attributes get sorted alphabetically in the element. Take this example from here:

from xml.dom import minidom

# New document
xml = minidom.Document()

# Creates user element
userElem = xml.createElement("user")

# Set attributes to user element
userElem.setAttribute("name", "Sergio Oliveira")
userElem.setAttribute("nickname", "seocam")
userElem.setAttribute("email", "[email protected]")
userElem.setAttribute("photo","seocam.png")

# Append user element in xml document
xml.appendChild(userElem)

# Print the xml code
print xml.toprettyxml()

The result is this:

<?xml version="1.0" ?>
<user email="[email protected]" name="Sergio Oliveira" nickname="seocam" photo="seocam.png"/>

Which is all very well if you wanted the attributes in email/name/nickname/photo order instead of name/nickname/email/photo order as they were created.

How do you get the attributes to show up in the order you created them? Or, how do you control the order at all?

like image 252
hughdbrown Avatar asked Nov 16 '09 08:11

hughdbrown


1 Answers

According to the documentation, the order of attributes is arbitrary but consistent for the life of the DOM. This is common across DOM implementations. Sorry.

like image 165
Robert Christie Avatar answered Oct 09 '22 13:10

Robert Christie