Created
November 27, 2012 02:53
-
-
Save upa/4152086 to your computer and use it in GitHub Desktop.
xml2dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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