PythiloLesson 2 of 8 · progress kept in this browser

Read structured XML with ElementTree

XML represents nested elements with attributes and text. ElementTree parses that structure so code can address children and attributes without splitting strings around angle brackets. Escaping is handled by the serializer when creating output.

A missing element and an empty text value are different cases. Decide which the application accepts rather than letting an AttributeError define the behaviour accidentally. This exercise uses a small local XML document with a fixed schema. It reads only direct item children, making that scope explicit instead of collecting unrelated nested elements with a broad search.

from xml.etree import ElementTree as ET
root = ET.fromstring(
    '<items><item id="2">Tea</item></items>')
print([(item.attrib["id"], item.text)
       for item in root.findall("item")])
Your task

Implement xml_items(text): parse an <items> root, return direct <item> children as (int id, stripped text) tuples in order. Empty text becomes "". Reject a different root or a missing id.