Python ElementTree Guide for XML Processing
Python’s xml.etree.ElementTree module offers an
idiomatic, lightweight, and efficient API for parsing, creating, and
manipulating XML data. By modeling XML elements as standard Python
objects that mimic built-in data structures—treating hierarchical
elements like lists and attributes like dictionaries—it eliminates the
verbosity of traditional DOM parsers. This guide covers how
ElementTree integrates with core Python paradigms to
provide a clean and intuitive approach to XML processing.
Hierarchical Representation as Python Objects
The core strength of ElementTree is its representation
of XML documents as nested Element objects. Instead of
requiring complex method calls to traverse nodes,
ElementTree maps XML directly onto familiar Python
structures:
- List-like Sequence for Child Nodes: An
Elementacts as a sequence. You can access child elements by index (root[0]), slice them, check the child count withlen(root), or iterate directly over them using standardforloops. - Dictionary-like Storage for Attributes: Element
attributes are accessed directly through the
.attribdictionary, or via helper methods like.get('key'),.set('key', 'value'), and.keys(). - Direct Property Access for Content: Text content is
accessed and modified simply via the
.textand.tailstring attributes.
Efficient Parsing and Iteration
ElementTree provides multiple parsing strategies
tailored to different memory constraints and performance needs.
For in-memory parsing, ET.parse() reads files from disk,
while ET.fromstring() converts XML strings directly into
element trees:
import xml.etree.ElementTree as ET
# Parsing from a string
xml_data = "<catalog><book id='1'><title>Python Guide</title></book></catalog>"
root = ET.fromstring(xml_data)
# Natural iteration over children
for book in root:
print(book.attrib.get('id'), book.find('title').text)For large files that exceed available memory,
ElementTree provides ET.iterparse(). This
implements a generator-based, streaming pull parser, allowing elements
to be processed incrementally and cleared from memory as they are
read.
Native Search and XPath Support
The module integrates simplified XPath expressions to locate elements without requiring third-party libraries:
find(match): Returns the first subelement matching the tag or XPath expression.findall(match): Returns a list of all matching subelements.findtext(match, default=None): Finds the first matching element and returns its.textvalue directly.iter(tag=None): Recursively iterates over the entire subtree, yielding elements matching the specified tag, or all elements if no tag is provided.
# Find all title elements anywhere in the tree
for title in root.iter('title'):
print(title.text)
# Search using path expressions
featured_books = root.findall("./book[@featured='true']")Programmatic Construction and Modification
Creating and editing XML documents follows a clean, declarative
syntax using ET.Element and ET.SubElement:
# Constructing a new XML tree
root = ET.Element("data")
user = ET.SubElement(root, "user", id="101")
name = ET.SubElement(user, "name")
name.text = "Jane Doe"
# Modifying existing elements
user.set("active", "true")Serialization
Writing XML back to a file or string format is handled via
ET.ElementTree.write() or ET.tostring(). The
API supports specifying encodings (such as UTF-8) and formatting options
like XML declarations, integrating seamlessly with Python’s standard I/O
and byte-handling operations.