473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Minidom XML output - attributes in wrong order ?

I'm very new to Python, so consider this a silly newbie question...
Anyway, I'm building a small application to generate a XML. Using document
from minidom, I'm doing something like :

from xml.dom.minidom import Document

doc = Document()

c = doc.createElement("sometest")
doc.appendChild(c)
tmp = doc.createElement("info")
tmp.setAttribute("vehicle", "car")
tmp.setAttribute("x-ray ", "100-1")
tmp.setAttribute("age", "30")
c.appendChild(tmp)
print doc.toprettyxml(indent=" ")

What it then prints out is :

<?xml version="1.0" ?>
<sometest>
<info age="30" vehicle="car" x-ray ="100-1"/>
</sometest>

What I expected was : <info vehicle="car" x-ray="100-1" age="30"/>

So it seems the dom module sorts the attributes alphabetically. Is there any
way I can prevent it from doing that ? What I want is to list them out in
the same order as they are added in the code...

Thanks,

Peter
Sep 5 '06 #1
2 6331
So it seems the dom module sorts the attributes alphabetically. Is there any
way I can prevent it from doing that ? What I want is to list them out in
the same order as they are added in the code...
I don't know how to do what you ask, I'm just here to warn you that you
shouldn't rely on the order of attributes in the document in any way,
because this is implementation dependent.

If you need to preserve order of some items -- use child elements
instead of attributes.

--
Regards, Maxim Sloyko

Sep 5 '06 #2
Peter Møllerud wrote:
I'm very new to Python
then you might want to consider using ElementTree or lxml, not necessarily
minidom.

c = doc.createElement("sometest")
doc.appendChild(c)
tmp = doc.createElement("info")
tmp.setAttribute("vehicle", "car")
tmp.setAttribute("x-ray ", "100-1")
tmp.setAttribute("age", "30")
c.appendChild(tmp)
print doc.toprettyxml(indent=" ")

What it then prints out is :

<?xml version="1.0" ?>
<sometest>
<info age="30" vehicle="car" x-ray ="100-1"/>
</sometest>

What I expected was : <info vehicle="car" x-ray="100-1" age="30"/>
Attributes in XML are not ordered and no XML library will keep the order. All
you could do is serialise by hand, which is not that difficult either. Is
there any reason why you might want to keep the order?

Stefan
Sep 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that...
3
by: Sunil Movva | last post by:
I have an application that uses xml to communicate between threads. One of the threads in my app creates an xml message and sends it to a second thread. This second thread parses the message and...
5
by: Skip Montanaro | last post by:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails: >>> import xml.dom.minidom >>> d1 = xml.dom.minidom.parse("ES.xml") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
4
by: bkamrani | last post by:
Great guys: As a newbie, I'm trying to simply parse a xml file using minidom, but I don't know why I get some extra children(?). I don't know what is wrong in xml file, but I've tried different...
1
by: Paul Kozik | last post by:
I am trying to write a script that reads an XML file (using the minidom module), makes an edit to a few attributes, then saves it back. If I use minidom.Document() to create the xml file, then...
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
2
by: =?iso-8859-1?q?KLEIN_St=E9phane?= | last post by:
Hi, I've a xml svg file and I would like to update it with Python. First, I would like to fetch one dom node with getElementByID. I've one issue about this method. This is my example : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.