Skip to content

Instantly share code, notes, and snippets.

@bancek
Created September 15, 2012 20:53
Show Gist options
  • Save bancek/3729713 to your computer and use it in GitHub Desktop.
Save bancek/3729713 to your computer and use it in GitHub Desktop.
XML to JSON
import json
from collections import OrderedDict
from lxml import objectify
def xml_to_py(el):
if len(set([x.tag for x in el.getchildren()])) == 1:
return [xml_to_py(x) for x in el.getchildren()]
if hasattr(el, 'pyval'):
return el.pyval
data = OrderedDict()
for key, value in el.attrib.items():
data[key] = value
for child in el.getchildren():
data[child.tag] = xml_to_py(child)
return data
xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movies>
<movie id="1">
<title>Movie 1</title>
<shows>
<show id="11">
<time>15:00:00</time>
</show>
<show id="12">
<time>17:15:00</time>
</show>
</shows>
</movie>
<movie id="2">
<title>Movie 2</title>
<shows>
<show id="21">
<time>16:00:00</time>
</show>
<show id="22">
<time>19:10:00</time>
</show>
</shows>
</movie>
</movies>
'''
py = xml_to_py(objectify.fromstring(xml))
json = json.dumps(py)
assert json == """[{"id": "1", "title": "Movie 1", "shows": [["15:00:00"], ["17:15:00"]]}, \
{"id": "2", "title": "Movie 2", "shows": [["16:00:00"], ["19:10:00"]]}]"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment