Advertisement
techhat

XML to Dict (Python)

Feb 17th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. def _xml_to_dict(xmltree):
  2.     '''
  3.    Convery an XML tree into a dict
  4.    '''
  5.     xmldict = {}
  6.     for item in xmltree:
  7.         comps = item.tag.split('}')
  8.         name = comps[1]
  9.         if not name in xmldict.keys():
  10.             if len(item.getchildren()) > 0:
  11.                 xmldict[name] = _xml_to_dict(item)
  12.             else:
  13.                 xmldict[name] = item.text
  14.     return xmldict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement