Skip to content

Instantly share code, notes, and snippets.

@RPChinhara
Last active July 31, 2024 10:31
Show Gist options
  • Save RPChinhara/3a27feeb6b09e7b86bce312f855c97a2 to your computer and use it in GitHub Desktop.
Save RPChinhara/3a27feeb6b09e7b86bce312f855c97a2 to your computer and use it in GitHub Desktop.
XML to JSON
import xml.etree.ElementTree as ET
import json
def parse_element(element, namespace, indent=""):
result = {}
if element.attrib:
result['attributes'] = element.attrib
if element.text and element.text.strip():
result['text'] = element.text.strip()
for child in element:
child_data = parse_element(child, indent + " ")
tag = child.tag.replace('{' + f'{namespace}' + '}', '')
if tag in result:
if isinstance(result[tag], list):
result[tag].append(child_data)
else:
result[tag] = [result[tag], child_data]
else:
result[tag] = child_data
return result
def parse_xml(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
return parse_element(root, root.tag.split('}')[0].strip('{'))
if __name__=='__main__':
with open('path to json', 'w') as f:
json.dump(parse_xml('path to xml'), f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment