Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- xml= "<graphics type='xxx' port=’0’ autoport='xxx' listen='0.0.0.0'>
- <listen type='address' address='0.0.0.0'/>
- </graphics>"
- 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': {}}]}
- from lxml import etree
- dict(etree.fromstring(xml).items())
- {'autoport': 'xxx', 'type': 'xxx', 'port': '0', 'listen': '0.0.0.0'}
- from xml.etree import ElementTree as etree
- tree = etree.fromstring(xml)
- def xml_to_dict(tree):
- d={}
- if tree.text:
- d[tree.tag] = tree.text
- elif len(tree) < 0:
- d[tree.tag] = {}
- else:
- d[tree.tag] = map(xml_to_dict, tree)
- return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement