I need to parse a XML file and create a equivalent Dictionary for the same.
I have gone through previous Stack Overflow question for the same.
Converting xml to dictionary using ElementTree
Now I have a XML content as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Person>
<First>John</First>
<Last>Smith</Last>
<extra>
<details1>
<married>yes</married>
<status>rich</status>
</details1>
</extra>
</Person>
<Person>
<First>Jane</First>
<Last>Doe</Last>
<extra>
<details1>
<married>yes</married>
<status>rich</status>
</details1>
<details2>
<property>yes</property>
</details2>
</extra>
</Person>
</Data>
It is returning the output as follows:
[
{
"extra": "\n\t\t",
"Last": "Smith",
"First": "John"
}
]
[
{
"extra": "\n\t\t",
"Last": "Smith",
"First": "John"
},
{
"extra": "\n\t\t",
"Last": "Doe",
"First": "Jane"
}
]
So I need a way to parse the content of child element also and return the result please some one let me know the way for the same.
Current code:
import json
from lxml import etree
tree= etree.parse("dummy.xml")
root = tree.getroot()
datadict = []
for item in root:
d = {}
for elem in item:
d[elem.tag]=elem.text
datadict.append(d)
new_d = json.dumps(datadict, indent=1)
print new_d