开发者

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 impor开发者_如何学JAVAt 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 this:

<?xml version="1.0" ?>
<user email="seocam@taboca.com" 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜