lxml has been mentioned. You might also check out lxml.objectify for some really simple manipulation.

>>> from lxml import objectify
>>> tree = objectify.fromstring(your_xml)
>>> tree.weather.attrib["module_id"]
'0'
>>> tree.weather.forecast_information.city.attrib["data"]
'Mountain View, CA'
>>> tree.weather.forecast_information.postal_code.attrib["data"]
'94043'
Answer from Ryan Ginstrom on Stack Overflow
Top answer
1 of 9
19

lxml has been mentioned. You might also check out lxml.objectify for some really simple manipulation.

>>> from lxml import objectify
>>> tree = objectify.fromstring(your_xml)
>>> tree.weather.attrib["module_id"]
'0'
>>> tree.weather.forecast_information.city.attrib["data"]
'Mountain View, CA'
>>> tree.weather.forecast_information.postal_code.attrib["data"]
'94043'
2 of 9
10

You want a thin veneer? That's easy to cook up. Try the following trivial wrapper around ElementTree as a start:

# geetree.py
import xml.etree.ElementTree as ET

class GeeElem(object):
    """Wrapper around an ElementTree element. a['foo'] gets the
       attribute foo, a.foo gets the first subelement foo."""
    def __init__(self, elem):
        self.etElem = elem

    def __getitem__(self, name):
        res = self._getattr(name)
        if res is None:
            raise AttributeError, "No attribute named '%s'" % name
        return res

    def __getattr__(self, name):
        res = self._getelem(name)
        if res is None:
            raise IndexError, "No element named '%s'" % name
        return res

    def _getelem(self, name):
        res = self.etElem.find(name)
        if res is None:
            return None
        return GeeElem(res)

    def _getattr(self, name):
        return self.etElem.get(name)

class GeeTree(object):
    "Wrapper around an ElementTree."
    def __init__(self, fname):
        self.doc = ET.parse(fname)

    def __getattr__(self, name):
        if self.doc.getroot().tag != name:
            raise IndexError, "No element named '%s'" % name
        return GeeElem(self.doc.getroot())

    def getroot(self):
        return self.doc.getroot()

You invoke it so:

>>> import geetree
>>> t = geetree.GeeTree('foo.xml')
>>> t.xml_api_reply.weather.forecast_information.city['data']
'Mountain View, CA'
>>> t.xml_api_reply.weather.current_conditions.temp_f['data']
'68'
🌐
Python
docs.python.org › 3 › library › xml.html
XML Processing Modules — Python 3.14.3 documentation
Source code: Lib/xml/ Python’s interfaces for processing XML are grouped in the xml package. It is important to note that modules in the xml package require that there be at least one SAX-compliant...
Discussions

Is XML difficult to learn?
No. Really there is not much to learn about it. It is just a data format. More on reddit.com
🌐 r/AskComputerScience
2
0
April 12, 2019
reading xml with python3

The xml is gzip compressed - requests handles this automatically for you which you could use instead of urllib.

response = requests.get(url)
tree = etree.fromstring(response.content)

http://stackoverflow.com/a/26435241 discusses solutions for doing it with urllib

More on reddit.com
🌐 r/learnpython
4
11
September 24, 2012
How would I parse XML that I get through Pycurl?

Parsing any html or xml is easiest with BeautifulSoup. Just remember to specify that's it's XML, or it will default to HTML:

soup = bs4.BeautifulSoup(body, 'xml')
More on reddit.com
🌐 r/learnpython
4
0
November 9, 2011
Reading and writing out an XML file as flat file?

You probably want to write a SAX parser. Look at xml.sax in the stdlib.

More on reddit.com
🌐 r/Python
4
2
January 26, 2013
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Source code: Lib/xml/etree/ElementTree.py The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Tutorial: This is a short tutorial for using xml....
🌐
Real Python
realpython.com › python-xml-parser
A Roadmap to XML Parsers in Python – Real Python
September 25, 2023 - Its main advantage comes from being ... ... To start working with SAX in Python, you can use the same parse() and parseString() convenience functions as before, but from the xml.sax package instead....
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
January 12, 2026 - from bs4 import BeautifulSoup with open('dict.xml', 'r') as f: data = f.read() bs_data = BeautifulSoup(data, 'xml') for tag in bs_data.find_all('child', {'name':'Frank'}): tag['test'] = "WHAT !!" print(bs_data.prettify()) ... ElementTree is included in Python’s standard library, so no installation is required.
🌐
DataCamp
datacamp.com › tutorial › python-xml-elementtree
Python XML Tutorial: Element Tree Parse & Read | DataCamp
December 10, 2024 - Learn how you can parse, explore, modify and populate XML files with the Python ElementTree package, for loops and XPath expressions. ... Get your team access to the full DataCamp for business platform. Run and edit the code from this tutorial onlineRun code · As a data scientist, you'll find that understanding XML is powerful for both web-scraping and general practice in parsing a structured document.
🌐
Tutorialspoint
tutorialspoint.com › home › python › python xml processing
Python XML Processing
February 21, 2009 - The 'Element' class represents a single node in this tree. Reading and writing operations on XML files are done on the ElementTree level. Interactions with a single XML element and its sub-elements are done on the Element level.
Find elsewhere
🌐
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 advanced techniques like handling XML namespaces, performing XPath queries, and mapping XML data to custom Python objects.
🌐
LearnPython.com
learnpython.com › blog › read-xml-into-python
How to Read XML Files into Python | LearnPython.com
Now that we've grasped the basics of XML, let's delve into how we can efficiently read XML files into Python for further analysis and processing. We have a couple different options for libraries when working with XML in Python. In this section, we'll explore how to read XML files in Python using both built-in and third-party libraries.
🌐
OneUptime
oneuptime.com › home › blog › how to work with xml files in python
How to Work with XML Files in Python
January 25, 2026 - Learn how to read, write, parse, and manipulate XML files in Python using the built-in ElementTree module and the powerful lxml library. This guide covers practical examples for common XML operations.
🌐
Guru99
guru99.com › home › python › python xml file – how to read, write & parse
Python XML File – How to Read, Write & Parse
August 12, 2024 - Learn how to create,read, and parse XML Files in Python using minidom class and ElementTree. Python XML Tutorial with Example.
🌐
GeeksforGeeks
geeksforgeeks.org › xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - It works as a request-response ... Method 1: using bs4 and urllib. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files....
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
untangle is a simple library which takes an XML document and returns a Python object which mirrors the nodes and attributes in its structure. For example, an XML file like this: <?xml version="1.0"?> <root> <child name="child1"> </root> can ...
🌐
ScrapingAnt
scrapingant.com › blog › python-parse-xml
How to Parse XML in Python | ScrapingAnt
August 2, 2024 - These built-in Python XML parsers offer a good balance of speed and ease of use for most applications. ... Consider using lxml, a fast and feature-rich Python library that combines the power of libxml2 and libxslt (lxml documentation). lxml is particularly efficient for parsing large XML documents and provides additional functionality like XPath support. ... Utilize event-driven parsing with xml.parsers.expat or lxml.etree.iterparse() to process XML data without loading the entire document into memory (xml.parsers.expat documentation).
🌐
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
It has several key steps such as checking the syntax of the XML document, tokenizing, and building the document structure in a hierarchy. XML parsing is surprisingly difficult if you've worked with any XML document before, then you might already ...
Author   oxylabs
🌐
Edureka
edureka.co › blog › python-xml-parser-tutorial
How to Parse and Modify XML in Python?
December 5, 2024 - To bind XML data to Python objects, you can define models using XPath expressions or generate models from an XML schema. Both approaches enable you to create Python classes that correspond to the XML structure and easily interact with the XML data.
🌐
Oxylabs
oxylabs.io › blog › python-parse-xml
How to Parse XML in Python
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 files directly to a Python dictionary object. ... The cool thing about this library is you can pass a URL, filename, or even an XML string to the `parse,` and it’ll still work.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-parse-xml-in-python-without-using-external-libraries
How to Parse XML in Python Without Using External Libraries
November 12, 2025 - We convert numeric strings to proper types (float for price, int for stock) For nested categories, we first check if the <categories> element exists. Then we iterate through child <category> elements and collect their text · The result is clean Python data structures you can easily work with. You can now use the parser like so: products = parse_product_catalog('products.xml') for product in products: print(f"\nProduct: {product['name']}") print(f" ID: {product['id']}") print(f" Price: {product['currency']} {product['price']}") print(f" Stock: {product['stock']}") print(f" Categories: {', '.join(product['categories'])}")
🌐
Rdegges
rdegges.com › 2013 › quickly-extract-xml-data-with-python
Randall Degges - Quickly Extract XML Data with Python
We then use the find method, passing in an XPath selector, which allows us to specify what element we’re trying to extract. If the element can’t be found, None is returned. If the element can be found, then we’ll use the .text property on our element object to grab the data out of the desired XML element.
🌐
Medium
mjamilmoughal.medium.com › working-with-xml-using-python-933e39598581
Working with XML using Python. In this article we are going to learn… | by Jamil Moughal | Medium
April 17, 2021 - Python have a build in library ElementTree, that has functions to read and manipulate XML files. First of all we have to import ElementTree. It is a common practice to use ET alias.