XML attributes are sorted

When I create a document using the mini-disk, the attributes are 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", "seocam@taboca.com")
userElem.setAttribute("photo","seocam.png")

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

# Print the xml code
print xml.toprettyxml()

The result is the following:

<?xml version="1.0" ?>
<user email="seocam@taboca.com" name="Sergio Oliveira" nickname="seocam" photo="seocam.png"/>

That is all very well if you wanted to use the attributes in the letter / name / pseudonym / order of the photo instead of the name / nickname / email address / order of photos as they are created.

How do you get the attributes to display in the order in which you created them? Or, how do you generally control the order?

+5
source share
1 answer

, , DOM. DOM. .

+5

All Articles