If parsing speed is a key factor for you, consider using cElementTree or lxml.

There are definitely more options out there, see these threads:

  • What is the fastest way to parse large XML docs in Python?
  • How do I parse XML in Python?
  • High-performance XML parsing in Python with lxml
Answer from alecxe on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-xml-parser
A Roadmap to XML Parsers in Python โ€“ Real Python
September 25, 2023 - The pull parser offers an interesting alternative to DOM and SAX by combining the best of both worlds. Itโ€™s efficient, flexible, and straightforward to use, leading to more compact and readable code. You could also use it to process multiple XML files at the same time more easily. That said, none of the XML parsers mentioned so far can match the elegance, simplicity, and completeness of the last one to arrive in Pythonโ€™s standard library.
Discussions

What is a good XML stream parser for Python? - Stack Overflow
Are there any XML parsers for Python that can parse file streams? My XML files are too big to fit in memory, so I need to parse the stream. Ideally I wouldn't have to have root access to install t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
beautifulsoup - For web scraping and xml parsing, which is best library to learn - Stack Overflow
I am getting confused with multiple libraries for the same work. I want to learn to one library which will handle both xml and html parsing. Do elementtree is compatible for html parsing. I heard a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Best python libraries to manage XML files

https://lxml.de/

More on reddit.com
๐ŸŒ r/Python
8
17
January 30, 2017
A Roadmap to XML Parsers in Python โ€“ Real Python
Heh, this would have been so useful for me a week ago, when I needed to parse an XML. Found xmltodict and it served its purpose beautifully - for what I needed at least. More on reddit.com
๐ŸŒ r/Python
14
88
October 19, 2021
๐ŸŒ
ScrapingAnt
scrapingant.com โ€บ blog โ€บ python-parse-xml
How to Parse XML in Python | ScrapingAnt
August 2, 2024 - Discover the best Python libraries for XML parsing, including xml.etree.ElementTree, lxml, BeautifulSoup, and more. Learn how to use each with code examples and performance comparisons. When it comes to XML parsing in Python, selecting the appropriate parser is crucial for optimal performance ...
๐ŸŒ
The Hitchhiker's Guide to Python
docs.python-guide.org โ€บ scenarios โ€บ xml
XML parsing โ€” The Hitchhiker's Guide to Python
xmltodict also lets you roundtrip back to XML with the unparse function, has a streaming mode suitable for handling files that donโ€™t fit in memory, and supports XML namespaces.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ xml.html
XML Processing Modules โ€” Python 3.14.3 documentation
The built-in XML parsers of Python rely on the library libexpat, commonly called Expat, for parsing XML.
๐ŸŒ
Close
making.close.com โ€บ posts โ€บ state-of-xml-parsing-in-python
State of XML Parsing in Python | The Making of Close
So lxml still looks like the best fit for the time being... even if the pain points are still there. ... Is not coupled to a single parser. Maybe libxml2 is a good enough fit, but would be nice to plug alternative XML parsers or even HTML parsers. Sometimes you need a spec compliant parser, other times you need a loose HTML5 parser. These might have Python or more performant implementations in different languages
Find elsewhere
Top answer
1 of 3
23

Here's good answer about xml.etree.ElementTree.iterparse practice on huge XML files. lxml has the method as well. The key to stream parsing with iterparse is manual clearing and removing already processed nodes, because otherwise you will end up running out of memory.

Another option is using xml.sax. The official manual is too formal to me, and lacks examples so it needs clarification along with the question. Default parser module, xml.sax.expatreader, implement incremental parsing interface xml.sax.xmlreader.IncrementalParser. That is to say xml.sax.make_parser() provides suitable stream parser.

For instance, given a XML stream like:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <entry><a>value 0</a><b foo='bar' /></entry>
  <entry><a>value 1</a><b foo='baz' /></entry>
  <entry><a>value 2</a><b foo='quz' /></entry>
  ...
</root>

Can be handled in the following way.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.sax


class StreamHandler(xml.sax.handler.ContentHandler):

  lastEntry = None
  lastName  = None


  def startElement(self, name, attrs):
    self.lastName = name
    if name == 'entry':
      self.lastEntry = {}
    elif name != 'root':
      self.lastEntry[name] = {'attrs': attrs, 'content': ''}

  def endElement(self, name):
    if name == 'entry':
      print({
        'a' : self.lastEntry['a']['content'],
        'b' : self.lastEntry['b']['attrs'].getValue('foo')
      })
      self.lastEntry = None
    elif name == 'root':
      raise StopIteration

  def characters(self, content):
    if self.lastEntry:
      self.lastEntry[self.lastName]['content'] += content


if __name__ == '__main__':
  # use default ``xml.sax.expatreader``
  parser = xml.sax.make_parser()
  parser.setContentHandler(StreamHandler())
  # feed the parser with small chunks to simulate
  with open('data.xml') as f:
    while True:
      buffer = f.read(16)
      if buffer:
        try:
          parser.feed(buffer)
        except StopIteration:
          break
  # if you can provide a file-like object it's as simple as
  with open('data.xml') as f:
    parser.parse(f)
2 of 3
12

Are you looking for xml.sax? It's right in the standard library.

๐ŸŒ
Zyte
zyte.com โ€บ learn โ€บ a-practical-guide-to-xml-parsing-with-python
A Practical Guide to Python XML Parsing
In this guide, weโ€™ll dive into the world of XML parsing using ยท Python, providing not just basic methods but also advanced techniques like handling XML namespaces, performing XPath queries, and mapping XML data to custom Python objects.
๐ŸŒ
GitHub
github.com โ€บ oxylabs โ€บ how-to-parse-xml-in-python
GitHub - oxylabs/how-to-parse-xml-in-python: Follow this in-depth technical tutorial to learn how to parse XML data in Python, what libraries you should use, how to handle invalid XML, and more. ยท GitHub
This library is not only fast but ... with Python's ElementTree. Many libraries such as Beautiful Soup can also utilize the lxml parser under the hood to get a performance boost....
Author ย  oxylabs
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - Our goal is to process this RSS feed (or XML file) and save it in some other format for future use. Python Module used: This article will focus on using inbuilt xml module in python for parsing XML and the main focus will be on the ElementTree XML API of this module.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ xml.etree.elementtree.html
xml.etree.ElementTree โ€” The ElementTree XML API
January 29, 2026 - For fully non-blocking parsing, see XMLPullParser. ... iterparse() only guarantees that it has seen the โ€œ>โ€ character of a starting tag when it emits a โ€œstartโ€ event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present. If you need a fully populated element, look for โ€œendโ€ events instead. Deprecated since version 3.4: The parser argument.
๐ŸŒ
FileFormat
blog.fileformat.com โ€บ fileformat.blogs โ€บ the best xml parsers for python, java, and javascript (with examples)
The Best XML Parsers for Python, Java, and JavaScript (With Examples)
March 21, 2025 - Once youโ€™ve selected a parser, implement it using our guide on how to read and edit XML files in Python, Java, and JavaScript. Each parser has its own advantages depending on your use case. If youโ€™re working with small XML files, ElementTree or DOM is great. For large files, use SAX or ...
๐ŸŒ
Oxylabs
oxylabs.io โ€บ blog โ€บ python-parse-xml
How to Parse XML in Python
`lxml` is arguably the fastest parsing library with support for Xpath, XSLT & XML Schema standards. It's the Python binding for the C libraries libxml2 and libxslt. This library is fast and packs a familiar interface as ElementTree API with ...
๐ŸŒ
Nick Janetakis
nickjanetakis.com โ€บ blog โ€บ how-i-used-the-lxml-library-to-parse-xml-20x-faster-in-python
How I Used the lxml Library to Parse XML 20x Faster in Python โ€” Nick Janetakis
August 20, 2019 - It's packed with best practices and examples. Start Learning Docker โ†’ ... Not too long ago I was writing a Flask service for a client that had to interact with a SOAP API (gross, I know), and one of the goals of this service was to take a bunch of XML data and then compare -> manipulate -> save it to a database. Most requests were less than 20MB in which case the first solution I used (which was the xmltodict Python library) was fine and dandy but once I had to deal with 400mb of data things got quite slow.
๐ŸŒ
lxml
lxml.de
lxml - Processing XML and HTML with Python
lxml - the most feature-rich and easy-to-use library for processing XML and HTML in the Python language
๐ŸŒ
ProxiesAPI
proxiesapi.com โ€บ articles โ€บ what-is-the-fastest-xml-parser-in-python
What is the fastest XML parser in Python? | ProxiesAPI
Choosing the right XML parsing library is crucial for performance. lxml is the fastest option, taking only 0.35 seconds compared to over 2 seconds with xml.etree.ElementTree. It's well worth the extra setup.
๐ŸŒ
Bright Data
brightdata.com โ€บ blog โ€บ how-tos โ€บ parsing-xml-in-python
How to Parse XML in Python? Multiple Methods Covered
September 16, 2025 - However, if your analysis requires understanding the relationships between different data segments, SAX may not be the best choice. untangle is a lightweight XML parsing library for Python that simplifies the process of extracting data from XML documents. Unlike traditional XML parsers that require navigating through hierarchical structures, untangle lets you access XML elements and attributes directly as Python objects.
๐ŸŒ
Decodo
decodo.com โ€บ blog โ€บ parse-xml-python
Learn How to Parse XML in Python
However, the two most widely used XML parsers in the Python ecosystem are ElementTree (xml.etree.ElementTree) and lxml.