0

i tried to reserve xml elementree in this format

from lxml import etree
box = etree.SubElement(image, 'box',top=t,left=l,width=w,height=h)

but what i got is

<box height="511" left="1" top="1" width="510">

I noticed that the attribute are in alphabetical order so what should I do to put it in order that I did?

Essalah Souad
  • 63
  • 1
  • 6
  • 1
    The question is tagged "elementtree" but in the code you use lxml. I cannot reproduce the problem with **lxml**. When serializing the XML document, the attributes appear in the order they were added (see also https://stackoverflow.com/a/17654556/407651). But with the **xml.etree.ElementTree** standard module, I do get the same output as you. – mzjn Sep 23 '19 at 10:19

1 Answers1

1

In XML, the order of attributes is of no concern by specification. So it is not mandatory to keep the order of the attributes.

Your only option would be testing other XML parsers and maybe you'll find one that doesn't change the order (what depends on the inner implementation).

This SO answer elaborates on that.

zx485
  • 28,498
  • 28
  • 50
  • 59