Order xml attributes

I use Python and ElementTree to change some attributes of XML files. Everything works fine, but the order of the attributes of the modified files is no longer the same as before. To maintain order, I tried it with this hack:

ordered_keys = ('t', 's', 'sp', 'id', 'le')

for k in ordered_keys:
    if k in sp.attrib:
        sp.set(k, sp.attrib[k])

tree.write("output.xml", encoding='utf-8', xml_declaration=True)

But this also did not work :( Do you have an idea to save the order?

+4
source share
2 answers

XML node attributes unordered definition . In other words, it does not matter in which attributes of the order are followed.

See also:

+2
source

- , .

, , , . , , ( ) ElementTree . ElementTree.py xml.etree lib.

-, Element , . , . ~ line 450 python 2.6. py2.7 + , py2.6 backported .

-, ( ). . ElementTree.write(), - items.sort(). . ~ 688 python 2.6.

. ( ), :

_start _start_list ( XMLTreeBuilder), attrib = {}, .

+2

Source: https://habr.com/ru/post/1545694/


All Articles