I have nested python dictionary like this.
d = {}
d[a] = b
d[c] = {1:2, 2:3}
I am trying to recursively convert the nested dictionary into an xml format since there can be more nested dictionary inside such as d[e] = {1:{2:3}, 3:4}
. My desired XML format is like this
<root>
<a>b</a>
<c>
<1>2</1>
<2>3</3>
</c>
</root>
I have so far this python code to handle nested xml using lxml library. But it doesn't give me the desired output.
def encode(node, Dict):
if len(Dict) == 0:
return node
for kee, val in Dict.items():
subNode = etree.SubElement(node, kee)
del msgDict[kee]
if not isinstance(val, dict):
subNode.text = str(val)
else:
return encode(subNode, val)
Any help is appreciated. Thank you.