python - Parse a XML file and convert to Dict -
i need parse xml file , create equivalent dictionary same.
i have gone through previous stack overflow question same.
converting xml dictionary using elementtree
now have xml content 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 returning output 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 need way parse content of child element , return result please 1 let me know way same.
current code:
import json lxml import etree tree= etree.parse("dummy.xml") root = tree.getroot() datadict = [] item in root: d = {} elem in item: d[elem.tag]=elem.text datadict.append(d) new_d = json.dumps(datadict, indent=1) print new_d
have @ the docs. might want iterate on children this:
[ child.tag child in item.iterchildren() ]
Comments
Post a Comment