I want to extract attributes out of the xml file using this code
xml file is :
xml= "<graphics type='xxx' port=’0’ autoport='xxx' listen='0.0.0.0'> <listen type='address' address='0.0.0.0'/> </graphics>"
and the code is :
def xml_to_dict(xml): d={} if xmlk.text: d[xmlk.tag] = xmlk.text else: d[xmlk.tag] = {} children = xmlk.getchildren() if children: d[xmlk.tag] = map(xml_to_dict, children) return d xml_to_dict(xyz) Output: {'graphics': [{'listen': {}}]}
i have tried dmlk,attrib instead of tag but to no avail. Does anybody knows this
I’d suggest lxml, but the following code works with either lxml or ElementTree
You also need to tweak your agorithm a bit:
That gives you what you asked for.