Reading an XML file with ElementTree

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading an XML file with ElementTree

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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')
2. Which line gets the root element from a parsed tree?
A root = tree.getroot()
B root = tree.root.get()
3. You want every <student> element directly inside root. Which call is correct?
A root.findall('student')
B root.find('student')
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
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'):

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1root = ET.parse('students.xml').getroot() AReturns a list of every child element of root that is named student
2root.findall('student') BParses the XML file into a tree and stores its top-level (root) element in a variable
3Use find() to reach one child element, then .text to read the value stored inside it CMissing .text, so this prints the element object rather than the value inside it. Fix: student.find('name').text
4Use .get() on an element to read the value of one of its attributes Did = student.get('id')
5print(student.find('name')) Efind() returns only the first matching element, not a list to loop over. Fix: root.findall('student')
6for s in root.find('student'): Fname = student.find('name').text

Definitions

xml.etree.ElementTree

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import xml.etree.ElementTree as ET tree = ET._____("students.xml")
2.
tree = ET.parse("students.xml") root = tree._______()
3.
for s in root._______("student"): print(s.find("name").text)
4.
for s in root.findall("student"): name = s.____("name").text
5.
for s in root.findall("student"): print(s.___("id"))

Spot the Error

Shade in the letter of the line that contains an 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)
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)
Question 3
A tree = ET.parse('class.xml')
B root = tree.getRoot()
C for s in root.findall('kid'):
D print(s.get('id'))
Question 4
A tree = ET.parse('cars.xml')
B root = tree.getroot()
C car = root.findall('car')
D print(car.find('make').text)
Question 5
A tree = ET.parse('menu.xml')
B root = ET.getroot()
C item = root.find('item')
D print(item.find('cost').text)

Answer Key

Reading an XML file with ElementTree
Section A: Quick Questions
Q1
A — tree = ET.parse('students.xml')
Q2
A — root = tree.getroot()
Q3
A — root.findall('student')
Q4
B — item.find('name').text
Q5
B — for s in root.findall('student'):
Section B: Match the Pairs
Q1
root = ET.parse('students.xml').getroot() → Parses the XML file into a tree and stores its top-level (root) element in a variable
Q2
root.findall('student') → Returns a list of every child element of root that is named student
Q3
Use find() to reach one child element, then .text to read the value stored inside it → name = student.find('name').text
Q4
Use .get() on an element to read the value of one of its attributes → id = student.get('id')
Q5
print(student.find('name')) → Missing .text, so this prints the element object rather than the value inside it. Fix: student.find('name').text
Q6
for s in root.find('student'): → find() returns only the first matching element, not a list to loop over. Fix: root.findall('student')
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
import xml.etree.ElementTree as ET tree = ET.parse("students.xml")
Q2
tree = ET.parse("students.xml") root = tree.getroot()
Q3
for s in root.findall("student"): print(s.find("name").text)
Q4
for s in root.findall("student"): name = s.find("name").text
Q5
for s in root.findall("student"): print(s.get("id"))
Section E: Spot the Error
Q1
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().
Q2
C — find() returns only the first matching element, not a list of all matches. To loop over every <pet> element use root.findall('pet').
Q3
B — Python method names are case-sensitive. The ElementTree method is getroot() all in lowercase, so getRoot() causes an AttributeError.
Q4
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.
Q5
B — getroot() belongs to the tree object returned by ET.parse(), not to the ET module. It should be root = tree.getroot().