I am trying to use dict2xml to convert a nested dictionary to xml.
This code:
from dict2xml import dict2xml
data = {
'a': 1,
'b': [2, 3],
'c': {
'd': [
{'p': 9},
{'o': 10}
],
'e': 7
}
}
print dict2xml(data, wrap="all", indent=" ")
Generated a correct xml like this:
<all>
<a>1</a>
<b>2</b>
<b>3</b>
<c>
<d>
<p>9</p>
</d>
<d>
<o>10</o>
</d>
<e>7</e>
</c>
</all>
However, if I change 'd' --> 'z', and maintains the order of keys by data = collections.OrderedDict(data), the order in the xml is incorrect and 'z' ends up after 'e' under 'c' in the xml, like this:
<all>
<a>1</a>
<b>2</b>
<b>3</b>
<c>
<e>7</e>
<z>
<p>9</p>
</z>
<z>
<o>10</o>
</z>
</c>
</all>
How can I run dict2xml without sorting the order of keys? Is there another solution to make a xml from my dict?
Thanks!