ElementTree represents elements, attributes, and text as a tree. It handles integrations, feeds, and legacy formats without an extra dependency for basic operations.
Practical example
from xml.etree.ElementTree import fromstring
xml = "<orders><order id='A1'><total>42.50</total></order></orders>"
root = fromstring(xml)
order = root.find("order")
if order is None:
raise ValueError("missing order")
print(order.attrib["id"], order.findtext("total"))
Queries and namespaces
find() may return None, so validate required elements before using their data. For namespaced documents, pass a prefix map instead of stripping namespace URIs manually.
Bound external input
Do not assume received XML is small or safe. Limit size and depth, and consider a hardened library for hostile XML or advanced requirements. Validate field types and business rules after parsing too.
Keep learning
Continue with Python Pathlib: Complete Guide to File System Manipulation and Python Unicode, Bytes, and UTF-8: Practical Guide. A official Python documentation, accessed July 22, 2026, documents the API and behavior across versions.