Skip to content

Instantly share code, notes, and snippets.

@Ceasar
Created July 26, 2013 22:44
Show Gist options
  • Save Ceasar/6092751 to your computer and use it in GitHub Desktop.
Save Ceasar/6092751 to your computer and use it in GitHub Desktop.
Convert xml to json.
'''A module for converting xml into json.'''
import json
from lxml import etree
def xml_to_json(xml_input, json_output):
'''Converts an xml file to json.'''
dict_to_json(etree_to_dict(xml_to_etree(xml_input), True), json_output)
def xml_to_etree(xml_input):
'''Converts xml to a lxml etree.'''
f = open(xml_input, 'r')
xml = f.read()
f.close()
return etree.HTML(xml)
def etree_to_dict(tree, only_child):
'''Converts an lxml etree into a dictionary.'''
mydict = dict([(item[0], item[1]) for item in tree.items()])
children = tree.getchildren()
if children:
if len(children) > 1:
mydict['children'] = [etree_to_dict(child, False) for child in children]
else:
child = children[0]
mydict[child.tag] = etree_to_dict(child, True)
if only_child:
return mydict
else:
return {tree.tag: mydict}
def dict_to_json(dictionary, json_output):
'''Coverts a dictionary into a json file.'''
f = open(json_output, 'w')
f.write(json.dumps(dictionary, sort_keys=True, indent=4))
f.close()
Copy link

ghost commented Jun 22, 2017

I have not see place/code, how to pass .xml file(myxml.xml) into this script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment