Python XML processing tutorial
Although JSON now dominates front-end and back-end interactions on the Web due to its lightweight, easy-to-read and write features, XML’s structural constraints, namespaces, mixed content and other capabilities make it still irreplaceable and is commonly used in enterprise applications, configuration files, industry data exchange and other scenarios.
This article will help you quickly get started with Python's built-in XML parsing and generation tools, and demonstrate the complete process using a real weather interface.
1. XML you can see
You must have come into contact with the following use cases, they are all important "bases" of XML:
- Android
activity_layout.xmllayout file - RSS 2.0/Atom news feed
- Word
.docx、Excel.xlsxThe contents of the underlying compressed package - SOAP interface request/response for banking, insurance and other industries
- Jenkins Pipeline configuration (commonly used for old versions or complex requirements)
-Maven's
pom.xml, Web applicationweb.xml
These scenarios either require strict format validation or namespace isolation, which is what XML excels at.
2. Python built-in parser comparison
The Python standard library provides two core parsing solutions. There is no absolute good or bad, only scene adaptation.
DOM(Document Object Model)
"Load the entire document tree into memory and change it as you want"
✅ ADVANTAGES
- Supports random access to any node, and it is very convenient to jump between father, son, and sibling nodes.
- You can directly modify, add, and delete XML structures
- Python built-in
xml.dom.minidomIt is a lightweight DOM implementation, suitable for novices
❌ Disadvantages
- Memory consumption is directly proportional to the size of the XML file. Large files (more than 100 MB) may directly burst the memory.
- Parsing is slower than event-driven solutions
SAX(Simple API for XML)
"Parse while reading, trigger events when encountering elements, and never look back."
✅ ADVANTAGES
- Extremely low memory usage (only saves the current context), can handle GB-level files
- Extremely fast parsing speed, implemented in C language
xml.parsers.expatExcellent performance
❌ Disadvantages
- Only one-way sequential reading can be done, and previous nodes cannot be traced back or modified.
- You need to write your own event processing logic (such as tracking the current label path)
📌 Practical priority rule: Use DOM for small files and need to modify the structure; for large files and read-only parsing, SAX is preferred. This article focuses on high-performance and versatile SAX, and also briefly introduces the usage of DOM.
3. Get started with SAX quickly
The core idea of SAX: Register three hook functions → Feed XML data → The parser automatically triggers the hook.
Three hooks correspond to three key events:
- Encountered start tag (such as
<book>,<title lang="en">) - Encountering Plain text/CDATA (text content in tags)
- Encountered end tag (such as
</book>,</title>)
3.1 Basic example: traverse all nodes
After running it you will see indented structured output that looks almost the same as the original XML.
3.2 Advanced techniques: processing segmented text
⚠️ Important pitfalls: SAXCharacterDataHandlerThe complete text within the tag will not be returned in one go. Multiple callbacks may be triggered if the text contains newlines, special characters, or if the parser's internal buffer is full.
Solution: Temporarily cache the text fragments in memory, and then splice them when the end tag is used.
This way, even if the text is split, what you end up with is the complete label content.
4. How to generate XML?
The simplest way to generate XML is string concatenation, but you must pay attention to escaping special characters (such as& → &、< → <), otherwise the generated document may be illegal.
4.1 Simple scenario: string concatenation + escaping
Using the standard libraryxml.sax.saxutils.escape()Basic escaping can be handled automatically:
4.2 Complex scenes: using ElementTree
When the XML structure is multi-layered and has many attributes, handwritten strings are prone to errors. It is recommended to use the standard libraryxml.etree.ElementTree:
ElementTreeIt not only supports DOM-style random access, but also provides a convenient output interface, which is very suitable as a "Swiss Army Knife" in Python.
5. Practical combat: Parsing the real weather XML interface
We use the free WeatherAPI to demonstrate the complete parsing process of SAX. Please enterAPI_KEYReplace it with your own free key (or find another weather interface that provides XML output).
The whole process is: define event processing rules → obtain original XML from the network → SAX parsing → extract target data, clear and efficient.
6. Avoid pitfalls and best practices
- SAX text must be merged: do not trust directly
CharacterDataHandlerThe returned fragment is always assembled on the closing tag. - Special characters must be escaped: Must be used when generating XML
escape()(or ElementTree's built-in escaping), which the parser will automatically restore when parsing. - Set parsing restrictions to prevent attacks: Python 3.8+ has default restrictions on entity expansion, etc., but it is still recommended not to parse very large XML from unknown sources.
- High-frequency parsing recommended lxml: If you need XPath, complex namespace, and extremely high speed, you can introduce a third-party library
lxml, which is faster and more powerful than the standard library. - Don’t reinvent the wheel: The libraries for parsing XML are already very mature, so don’t parse them by handwriting string processing yourself.
7. When should you choose XML?
Even with the popularity of JSON, XML is still the best choice in the following scenarios:
- XSD/DTD structure verification required (such as financial, medical data exchange)
- Requires namespace to isolate tags with the same name (such as different versions of configuration files)
- Requires Mixed content (e.g. tags and text interleaved in XHTML)
- Connect to Legacy SOAP system
If you are building a new project, give priority to YAML for configuration, JSON for front-end and back-end interaction, and Protocol Buffers or MessagePack for high-performance cross-language transmission. And XML is still an unshakable standard in scenarios where you need "rigorous, orderly, and cross-organization exchange."

