loop through xml files in python

main.py
import os
import xml.etree.ElementTree as ET

# Directory containing the XML files
xml_dir = 'path/to/xml/files'

# Loop through each file in the directory
for filename in os.listdir(xml_dir):
    if filename.endswith('.xml'):
        file_path = os.path.join(xml_dir, filename)
        
        # Parse the XML file
        tree = ET.parse(file_path)
        root = tree.getroot()
        
        # Access the XML data as needed
        # Here you can access elements, attributes, text, etc.
        for elem in root.iter():
            print(elem.tag, elem.text)
566 chars
20 lines

related categories

gistlibby LogSnag