XML Processing
Python provides the xml.etree.ElementTree module as the easiest way to process XML data.
XML (eXtensible Markup Language) is still widely used in the enterprise world, web services (SOAP), application configuration files, and data exchange formats between systems. Although JSON is now more popular for modern APIs, XML remains the top choice in banking, healthcare, and government sectors because of its richer support for schemas (XSD), namespaces, and attributes.
xml.etree.ElementTree views an XML document as a tree of elements. Each element can have a tag, attributes, text, and child elements. This module supports parsing, navigating, modifying, and writing XML — all with a simple, Pythonic API. For large documents, use iterparse(), which processes XML incrementally without loading the entire file into memory.
In addition to ElementTree, Python also has xml.dom.minidom for a DOM-style approach, xml.sax for a memory-efficient streaming parser, and the third-party lxml, which is faster and feature-rich. However, for general needs, ElementTree is more than enough.
Code Example
import xml.etree.ElementTree as ET
# Creating XML
root = ET.Element("data")
item = ET.SubElement(root, "student")
item.set("id", "1")
ET.SubElement(item, "name").text = "John"
ET.SubElement(item, "score").text = "85"
tree = ET.ElementTree(root)
tree.write("data.xml", encoding="utf-8", xml_declaration=True)
# Reading XML
tree = ET.parse("data.xml")
root = tree.getroot()
for student in root.findall("student"):
print(student.find("name").text)
Code Explanation
First, import xml.etree.ElementTree as ET imports the module with the alias ET. The root element is created with ET.Element("data"), then ET.SubElement(root, "student") adds a child "student" element under root. The id attribute is added with .set("id", "1").
The "name" and "score" elements are added as sub-elements of "student", and their text is set directly via .text. The whole structure is wrapped into ET.ElementTree(root) and written to an XML file with tree.write(), including UTF-8 encoding and the XML declaration.
When reading, ET.parse("data.xml") loads the file and returns an ElementTree object. .getroot() gets the root element, then .findall("student") finds all elements with the "student" tag. For each student, .find("name").text gets the text from the "name" sub-element.
Everyday Example
A school wants to store and retrieve student grade data in a format that can be exchanged with the Dapodik system. The administrator exports the data to XML with the structure: root <data>, each <student> has a student_id attribute, inside which are <name>, <class>, <score>. This file is sent to a central server for import.
With Python, simply parse the XML, validate each student element, extract the data, and then save it to the school database. If there is a formatting error, the program can immediately report which student has the problem. This approach is more structured and easier to audit than CSV.
Summary
xml.etree.ElementTree provides a simple and efficient way to create, read, and modify XML data in Python. Suitable for enterprise data exchange, configuration files, and legacy system integration. For heavier needs, consider lxml. Understand the tree structure, the findall/find methods, and attributes — that already covers 90% of everyday XML needs.