← VCE Software Development U3O1

Reading an XML file with ElementTree

Python module that parses an XML file into a tree of elements. Use find() and findall() to reach elements and .text to read their values.

Understanding Reading an XML file with ElementTree in VCE Software Development U3O1

What it does: find() returns the first element with that tag, and .text gives the string between its opening and closing tags. ET.parse() reads the XML file and builds the tree. ET has no read() function for this. Everything read from an XML file is a string. Use int() or float() before doing any arithmetic. The module path is xml.etree.ElementTree. The 'as ET' part gives it the short name ET. findall() collects all matching child elements, so it is used with a for loop to work through repeated records.

Common mistakes: getroot is a method, so it needs brackets to be called. Without them root holds the method itself instead of the root element. It should be root = tree.getroot(). find() returns only the first matching element, not a list of all matches. To loop over every <pet> element use root.findall('pet').

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 114–124) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly loads an XML file called students.xml into an ElementTree?
tree = ET.read('students.xml')tree = ET.parse('students.xml')
Matches
print(student.find('name'))
↓
Missing .text, so this prints the element object rather than the value inside it. Fix: student.find('name').text
Applying
Meanings
Term
xml.etree.ElementTree
Meaning
Python module that parses an XML file into a tree of elements. Use find() and findall() to reach elements and .text to read their values.
Practice
import xml.etree.ElementTree as ET
Testing
Gaps
tree = ET.("students.xml")
Check Answer
Spots
1tree = ET.parse('toys.xml')
2root = tree.getroot

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Golden Phoenix' badge — Savannah Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly loads an XML file called students.xml into an ElementTree?
A. tree = ET.parse('students.xml') ✓
B. tree = ET.read('students.xml')
ET.parse() reads the XML file and builds the tree. ET has no read() function for this.
2. Which line gets the root element from a parsed tree?
A. root = tree.getroot() ✓
B. root = tree.root.get()
getroot() is the method that returns the top-level element of the tree.
3. You want every <student> element directly inside root. Which call is correct?
A. root.findall('student') ✓
B. root.find('student')
findall() returns a list of all matching child elements. find() returns only the first match.
4. You want the text inside <name>Ava</name>, stored in the variable item. Which is correct?
A. item.find('name').text()
B. item.find('name').text ✓
text is an attribute, not a method, so it has no brackets. It holds the value between the tags.
5. Which loop correctly goes through each <student> element in root?
A. for s in root.find('student'):
B. for s in root.findall('student'): ✓
find() returns just one element (or None). To loop over every match, use findall() which returns a list.

Match the Pairs

root = ET.parse('students.xml').getroot() → Parses the XML file into a tree and stores its top-level (root) element in a variable
root.findall('student') → Returns a list of every child element of root that is named student
Use find() to reach one child element, then .text to read the value stored inside it → name = student.find('name').text
Use .get() on an element to read the value of one of its attributes → id = student.get('id')
print(student.find('name')) → Missing .text, so this prints the element object rather than the value inside it. Fix: student.find('name').text
for s in root.find('student'): → find() returns only the first matching element, not a list to loop over. Fix: root.findall('student')

Key Term

xml.etree.ElementTree
Python module that parses an XML file into a tree of elements. Use find() and findall() to reach elements and .text to read their values.

Fill in the Blanks

1. import xml.etree.ElementTree as ET tree = ET.parse("students.xml")
Hint: Type the ET function that reads an XML file and builds a tree from it
2. tree = ET.parse("students.xml") root = tree.getroot()
Hint: Type the method that returns the top-level element of the tree
3. for s in root.findall("student"): print(s.find("name").text)
Hint: Type the method that returns a list of every matching child element
4. for s in root.findall("student"): name = s.find("name").text
Hint: Type the method that returns only the first matching child element
5. for s in root.findall("student"): print(s.get("id"))
Hint: Type the element method that reads the value of an attribute

Spot the Error

Question 1
A tree = ET.parse('toys.xml')
B root = tree.getroot
C for t in root.findall('toy'):
D print(t.find('name').text)
Line B — getroot is a method, so it needs brackets to be called. Without them root holds the method itself instead of the root element. It should be root = tree.getroot().
Question 2
A tree = ET.parse('pets.xml')
B root = tree.getroot()
C for p in root.find('pet'):
D print(p.find('name').text)
Line C — find() returns only the first matching element, not a list of all matches. To loop over every <pet> element use root.findall('pet').
Question 3
A tree = ET.parse('class.xml')
B root = tree.getRoot()
C for s in root.findall('kid'):
D print(s.get('id'))
Line B — Python method names are case-sensitive. The ElementTree method is getroot() all in lowercase, so getRoot() causes an AttributeError.
Question 4
A tree = ET.parse('cars.xml')
B root = tree.getroot()
C car = root.findall('car')
D print(car.find('make').text)
Line C — findall() returns a list of elements, and a list has no find() method, so the next line would fail. Use root.find('car') to get a single element.
Question 5
A tree = ET.parse('menu.xml')
B root = ET.getroot()
C item = root.find('item')
D print(item.find('cost').text)
Line B — getroot() belongs to the tree object returned by ET.parse(), not to the ET module. It should be root = tree.getroot().

Student with a class code? Join your class →