parsing xml files using an element tree

Use the following code to extract ID attribute:

import xml.etree.ElementTree as ET
tree = ET.parse('a.xml')
root = tree.getroot()
for child in root:
    if child.tag == 'ShiftTypes':
        for i in child:
            print ('Here is the ID: ', i.attrib)
            for j in i:
                if j.tag == 'StartTime':
                    print ('Here is StartTime:', j.text)
                elif j.tag == 'EndTime':
                    print ('Here is EndTime:', j.text)
                elif j.tag == 'Description':
                    print ('Here is Description:', j.text)
Here is the ID:  {'ID': 'E'}
Here is StartTime: 06:30:00
Here is EndTime: 14:30:00
Here is Description: Early

Here is a useful tutorial about parsing XML data:

https://www.datacamp.com/community/tutorials/python-xml-elementtree

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top