views:

131

answers:

1

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?

+3  A: 

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.

Robert Christie