I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

import xml.etree.ElementTree as ET
root = ET.parse('thefile.xml').getroot()

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'):
    value = type_tag.get('foobar')
    print(value)

Output:

1
2
Answer from Alex Martelli on Stack Overflow
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Parses an XML section into an element tree incrementally, and reports what’s going on to the user. source is a filename or file object containing XML data. events is a sequence of events to report back. The supported events are the strings "start", "end", "comment", "pi", "start-ns" and "end-ns" ...
Discussions

Parse XML files in Python (ElementTree) - Geographic Information Systems Stack Exchange
I am trying to parse some xml and rss feeds to extract some of their data in order to store it in a PostGIS database. The file I want to parse is here: http://earthquake.usgs.gov/earthquakes/catal... More on gis.stackexchange.com
🌐 gis.stackexchange.com
March 6, 2012
How to process / parse XML with Python
Best bet: Use lxml . There's a nice tutorial on the site, too . You can also use the built-in XML parser, either with ElementTree (common) or XML DOM (rarer) , or any other related library from the xml module , but most people prefer lxml, as it's much more powerful (and in parts, compatible with ElementTree). More on reddit.com
🌐 r/learnpython
5
11
October 22, 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
Can someone explain to me how to parse an XML file?
The standard library has XML libraries, you could search in the docs on Python.org for those. The lxml library is also popular for this. Sorry I can't help with details, I haven't had to work with XML in a few years. More on reddit.com
🌐 r/learnpython
8
1
June 25, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - Load RSS feed from specified URL and save it as an XML file. Parse the XML file to save news as a list of dictionaries where each dictionary is a single news item.
🌐
DataCamp
datacamp.com › tutorial › python-xml-elementtree
Python XML Tutorial: Element Tree Parse & Read | DataCamp
December 10, 2024 - The XML file provided describes a basic collection of movies. The only problem is that the data is a mess! There have been many different curators of this collection, and everyone has their own way of entering data into the file. The main goal in this tutorial will be to read and understand the file with Python and then fix the problems. First, you need to read the file with ElementTree. tree = ET.parse('movies.xml') root = tree.getroot()
🌐
Real Python
realpython.com › python-xml-parser
A Roadmap to XML Parsers in Python – Real Python
September 25, 2023 - In this section, you’ll take a look at Python’s built-in XML parsers, which are available to you in nearly every Python distribution. You’re going to compare those parsers against a sample Scalable Vector Graphics (SVG) image, which is an XML-based format. By processing the same document with different parsers, you’ll be able to choose the one that suits you best. The sample image, which you’re about to save in a local file for reference, depicts a smiley face.
Top answer
1 of 4
6

Before I try to answer, a tip. Your exception handler covers up the nature of the problem. Just let the original exception rise up and you'll have more information to share with people who are interested in helping you.

I like to use feedparser to parse Atom feeds. It does indeed give you dict-like objects. I submitted a patch to feedparser 4.1 to parse the GeoRSS elements into GeoJSON style dicts. See https://code.google.com/p/feedparser/issues/detail?id=62 and blog post at http://sgillies.net/blog/566/georss-patch-for-universal-feedparser/. You'd use it like this:

>>> import feedparser
>>> feed = feedparser.parse("http://earthquake.usgs.gov/earthquakes/catalogs/1hour-M1.xml")
>>> feed.entries[0]['where']
{'type': 'Point', 'coordinates': (-122.8282, 38.844700000000003)}

My patched version of 4.1 is in my Dropbox and you can get it using pip.

$ pip install http://dl.dropbox.com/u/10325831/feedparser-4.1-georss.tar.gz

Or just download and install with "python setup.py install".

2 of 4
2

It's more comfortable to use lxml for XML processing. Here is an example that fetches the feed and prints earthquake titles and coordinates:

import lxml.etree

feed_url = 'http://earthquake.usgs.gov/earthquakes/catalogs/1hour-M1.xml'
ns = {
    'atom': 'http://www.w3.org/2005/Atom',
    'georss': 'http://www.georss.org/georss',
}

def main():
    doc = lxml.etree.parse(feed_url)
    for entry in doc.xpath('//atom:entry', namespaces=ns):
        [title] = entry.xpath('./atom:title', namespaces=ns)
        [point] = entry.xpath('./georss:point', namespaces=ns)
        print point.text, title.text

if __name__ == '__main__':
    main()
Find elsewhere
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
if the schema defines an element to be of type int, the parsed dict will contain also an int value for that element. Moreover the library supports automatic and explicit validation of XML documents against a schema. from xmlschema import XMLSchema, etree_tostring # load a XSD schema file schema = XMLSchema("your_schema.xsd") # validate against the schema schema.validate("your_file.xml") # or schema.is_valid("your_file.xml") # decode a file data = schmema.decode("your_file.xml") # encode to string s = etree_tostring(schema.encode(data))
🌐
Bright Data
brightdata.com › blog › how-tos › parsing-xml-in-python
How to Parse XML in Python? Multiple Methods Covered
September 16, 2025 - While it’s not as feature-rich or efficient as parsing with lxml, it offers a straightforward way to parse and manipulate XML data in Python. You can use the various methods available in the DOM object to access elements. For example, you can use the getElementsByTagName() method to retrieve the value of an element using its tag name. The following example demonstrates how to use the minidom library to parse an XML file and fetch the elements using their tag names:
🌐
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
First, we create a root XML document of the sample.xml file using the parse method. Then, we're iterating over all the child nodes and storing the data in a dict object. You can use the untangle library to convert XML documents directly to a Python dictionary object.
Author   oxylabs
🌐
ScrapingAnt
scrapingant.com › blog › python-parse-xml
How to Parse XML in Python | ScrapingAnt
August 2, 2024 - The minidom module implements a minimal DOM interface for XML parsing (Python documentation). While it's less efficient than ElementTree, it can be useful for small XML documents or when DOM compatibility is required. from xml.dom import minidom # Parse the XML file xmldoc = minidom.parse('example.xml') # Get a list of all 'item' elements itemlist = xmldoc.getElementsByTagName('item') # Print the number of 'item' elements print(len(itemlist))
🌐
Reddit
reddit.com › r/python › a roadmap to xml parsers in python – real python
r/Python on Reddit: A Roadmap to XML Parsers in Python – Real Python
October 19, 2021 - Instead, pass a custom lookup to the parser (trivial if using LXML's builtin xml/html parsing support; easy enough if you use bs4 as your frontend (just pass parser.makeelement). With html5lib I haven't figured out how yet; fortunately, large HTML files aren't common like large XML files are, so simply reserializing and parsing again works).
🌐
ProjectPro
projectpro.io › recipes › parse-xml-in-python
Python parse xml - Parse xml python - Projectpro
December 21, 2022 - Parsing XML in python using Minidom The World Wide Web Consortium's DOM (document object model) is a cross-language API for reading and changing XML content. Python allows you to parse XML files using xml.dom.minidom.
🌐
Scrapfly
scrapfly.io › blog › posts › how-to-parse-xml
How to Parse XML
September 26, 2025 - When it comes to parsing an XML file, there are a few options: Query languages, such as XPath and CSS. Native XML parses, such as ElementTree in Python.
🌐
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 - But the basic idea is we read in the sample.xml file and then parse it using 1 of the 3 strategies. We also use the default_timer function from Python’s timeit module to track how long it took to do the work.
🌐
Zyte
zyte.com › learn › a-practical-guide-to-xml-parsing-with-python
A Practical Guide to Python XML Parsing
Whether you're dealing with configuration files, SOAP-based APIs, or large datasets used in enterprise applications, understanding how to parse and manipulate XML is essential. In this guide, we’ll dive into the world of XML parsing using · Python, providing not just basic methods but also ...
🌐
lxml
lxml.de › tutorial.html
The lxml.etree Tutorial
These libraries usually provide a file-like object for the result that you can parse from while the response is streaming in. By default, lxml.etree uses a standard parser with a default setup. If you want to configure the parser, you can create a new instance: >>> parser = etree.XMLParser(remove_blank_text=True) # lxml.etree only!
🌐
Oxylabs
oxylabs.io › blog › python-parse-xml
How to Parse XML in Python
This is a common convention for ElementTree-based Python scripts. The `fromstring` method takes an XML string as an argument and returns the parsed ElementTree object. Next, you can iter over all the child nodes of the root node and print the texts using the below code: ... The Great Gatsby F. Scott Fitzgerald 1925 · So far, you’ve learned how to parse XML files from Python string objects.
🌐
Python
docs.python.org › 3 › library › pyexpat.html
xml.parsers.expat — Fast XML parsing using Expat
Due to limitations in the Expat ... a single XML document. Call ParserCreate for each document to provide unique parser instances. ... Home page of the Expat project. ... Parses the contents of the string data, calling the appropriate handler functions to process the parsed data. isfinal must be true on the final call to this method; it allows the parsing of a single file in fragments, ...
🌐
Board Infinity
boardinfinity.com › blog › xml-parsing-in-python
XML Parsing in Python | Board Infinity
August 9, 2025 - Reading and writing to/from files ... a single XML element and its children take place. Ok, so let’s go through the parseXML() function now:...