Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

XML to JSON with different XML contents

I want to convert multiple XML files to JSON files. Thing is, the XML files are always different, so they have different childs. My current code only gets the childs, not the values.
What I need:
  • Get childs and values from multiple xml files which can be different
  • Put that in a JSON file.
As said, my current code only gets the childs from the XML file:
import xml.etree.ElementTree as ET
from os import listdir, chdir
path = (r'C:\Users\path..')
chdir(path)
files = [f for f in listdir(path)]

elemlist = {}
for bestand in files:
    xmltree = ET.parse(bestand)
    for elem in xmltree.iter():
        elemlist[elem.tag] = elem.attrib
        #elemlist.append(elem.tag)

#elemlist = list(set(elemlist))

print(elemlist)

Comments