Skip to content

Instantly share code, notes, and snippets.

@thehajime
Forked from upa/xml2dict
Created October 29, 2013 09:28
Show Gist options
  • Save thehajime/7211481 to your computer and use it in GitHub Desktop.
Save thehajime/7211481 to your computer and use it in GitHub Desktop.
def xmltodict (node) :
def _xmltodict (node) :
if len (node) > 0:
dic = {}
for child in node :
[key, value] = _xmltodict (child)
if dic.has_key (key) :
if isinstance (dic[key], list) :
dic[key].append (value)
else :
tmplist = [dic[key], value]
dic[key] = tmplist
else :
dic[key] = value
return [node.tag, dic]
return [node.tag.strip (), node.text.strip()]
[domainkey, domaindict] = _xmltodict (node)
return domaindict
import xml.etree.ElementTree import ElementTree as etree
print xmltodict (etree (file = open ("hoge.xml")).getroot ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment