Minidom XML output - attributes in wrong order ?

Discussion in 'Python' started by Peter Møllerud, Sep 5, 2006.

  1. 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
     
    Peter Møllerud, Sep 5, 2006
    #1
    1. Advertising

  2. Peter Møllerud

    Maxim Sloyko Guest


    > 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
     
    Maxim Sloyko, Sep 5, 2006
    #2
    1. Advertising

  3. 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
     
    Stefan Behnel, Sep 9, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Wogan-Browne
    Replies:
    1
    Views:
    826
    Uche Ogbuji
    Jan 28, 2005
  2. Derek Basch
    Replies:
    2
    Views:
    467
    Derek Basch
    Mar 25, 2005
  3. Replies:
    3
    Views:
    546
    Stefan Behnel
    Aug 3, 2007
  4. Johannes Bauer
    Replies:
    7
    Views:
    1,076
    Johannes Bauer
    Jun 11, 2009
  5. ming
    Replies:
    2
    Views:
    186
Loading...

Share This Page