I'm using xmltodict for XML parsing/unparsing, and I need to preserve the XML element ordering while processing one document. Toy REPL example:
>>> import xmltodict
>>> xml = """
... <root>
... <a />
... <b />
... <a />
... </root>
... """
>>> xmltodict.parse(xml)
OrderedDict([('root', OrderedDict([('a', [None, None]), ('b', None)]))])
>>> xmltodict.unparse(_)
'<?xml version="1.0" encoding="utf-8"?>\n<root><a></a><a></a><b></b></root>'
Note that the original sequence [a, b, a]
is replaced by [a, a, b]
. Is there any way to preserve the original order with xmltodict
?