#!/usr/bin/env python3 import json import sys import xml.etree.ElementTree root = xml.etree.ElementTree.fromstring(sys.stdin.read()) assert root.tag == 'files' for file in root: assert file.tag == 'file' attributes = file.attrib childrenTags = [child.tag for child in file] assert sorted(childrenTags) == sorted(set(childrenTags)), 'duplicate children' children = {child.tag: child.text for child in file} assert not any(k in children for k in attributes), 'attribute found in children' assert not any(k in attributes for k in children), 'child found in attributes' assert 'name' in attributes, 'malformed file without name attribute' o = {'name': attributes['name']} del attributes['name'] o.update(sorted({**attributes, **children}.items())) print(json.dumps(o))