I have that XML-to-JSON function based on ElementTree
. It looks very simple but until now it does what it's supposed to do: give a JSON description of the document's ElementTree
.
import xml.etree.ElementTree as ET def dirtyParser(node): '''dirty xml parser parses tag, attributes, text, children recursive returns a nested dict''' # mapping with recursive call res = {'tag':node.tag, 'attributes': node.attrib, 'text': node.text, 'children': [dirtyParser(c) for c in node.getchildren()]} # remove blanks and empties for k, v in res.items(): if v in ['', '\n', [], {}, None]: res.pop(k, None) return res
Usage:
>>> some_xml = ET.fromstring(u'<?xml version="1.0" encoding="UTF-8" ?><records><record><him>Maldonado, Gavin G.</him><her>Veda Parks</her></record></records>') >>> dirtyParser(some_xml) >>> {'tag': 'records', 'children': [{'tag': 'record', 'children': [{'tag': 'him', 'text': 'Maldonado, Gavin G.'}, {'tag': 'her', 'text': 'Veda Parks'}]}]}
Is it really that reliable?