Skip to content

How to Parse XML in Python: ElementTree, lxml, and xmltodict

Most XML work in Python starts with the standard library and ends with a small decision: is this document trusted, is it large, and do you need a dictionary or full XPath? The guide below walks through that decision, then shows the concrete calls for reading XML, navigating elements, handling namespaces, streaming big files, converting to JSON, fetching from the web, and guarding against hostile input.

Which Python XML library should you use?

Start with xml.etree.ElementTree because it ships with Python and covers everyday parsing. Move to another library only when a specific requirement forces it: full XPath 1.0 or faster parsing points to lxml; a plain dictionary or JSON points to xmltodict; untrusted input points to defusedxml. The comparison table below lays out the trade-offs across the common options, including install command, XPath support, large-file behavior, and notes.

Parse XML with ElementTree: string and file

Two entry points cover most needs. ET.fromstring() takes an XML string and returns the root element directly. ET.parse() takes a file path and returns an ElementTree object; call .getroot() on it to reach the root element. Importing the module as ET is the common convention. Once you hold the root, the rest is tree navigation.

Access elements, attributes, and text

Every Element exposes three handles: .tag is the element name, .attrib is a dictionary of attributes, and .text is the string between the tags. Iterating an element visits its direct children. Use book.get("category") rather than book.attrib["category"] when an attribute may be absent: get() returns None or a default, while bracket access raises KeyError. Remember that .text is always a string, even when the value looks numeric, so cast explicitly before doing math.

Find and filter nodes: find(), findall(), iter(), and XPath

Three methods handle nearly all traversal. find() returns the first matching direct child. findall() returns a list of every matching direct child. iter() recurses through all descendants, so it reaches grandchildren and deeper. Because find() and findall() stop at the immediate child level, a tag that lives one level deeper will not appear — that scope difference, not a typo, is behind most surprising empty results.

ElementTree also understands a limited XPath dialect inside find() and findall(). A .// prefix matches at any depth, a predicate like [@attr='x'] filters on an attribute value, and a positional index such as tag[2] selects by order. This is a subset of XPath 1.0, so functions such as contains() are absent; when you need the full expression language, lxml is the upgrade path.

Handle XML namespaces: why findall() returns nothing

When findall() comes back empty on a document someone else produced, check for namespaces before anything else. ElementTree rewrites each namespaced tag into the {namespace-uri}tag form, so a bare findall("url") finds nothing in a sitemap whose tags are actually {http://www.sitemaps.org/schemas/sitemap/0.9}url. You can either query the expanded name or hand the parser a prefix-to-URI map. Because sitemaps and RSS/Atom feeds are namespaced by design, this mismatch accounts for a large share of confusing empty results.

If you edit a namespaced document and serialize it again, register the prefix up front with ET.register_namespace("", "http://www.sitemaps.org/schemas/sitemap/0.9"); otherwise the writer invents ns0:-style prefixes instead of preserving the original ones.

Parse large XML files faster with lxml and iterparse

When ElementTree becomes the bottleneck, lxml is a near drop-in replacement: it is written in C, parses significantly faster, supports full XPath 1.0, and its etree.parse(), find(), and findall() behave the same. For files too large to hold in memory, iterparse() streams the document element by element. Do not skip elem.clear(): if processed elements are never released, iterparse() keeps accumulating the full tree and the memory savings disappear.

Convert XML to a dict or JSON with xmltodict

Calling xmltodict.parse() on an XML string yields a dictionary shaped like the document: each attribute shows up as a key beginning with @, and element text that shares a parent with attributes is stored under #text. From that dictionary, json.dumps() gives you JSON in one step, and xmltodict.unparse() turns the dictionary back into XML. Skip xmltodict when the document mixes text and child elements inside the same parent, or when you need XPath queries; in those cases, stay with ElementTree or lxml.

Fetch and parse XML from the web: sitemaps, RSS, APIs

Most XMLs you parse in production are a sitemap, an RSS/Atom feed, or an API response behind an HTTP request, not a file on disk. Fetch it with requests, pass response.content to ET.fromstring(), then parse as usual. Pass response.content rather than response.text: raw bytes let the parser apply the encoding named in the XML prolog, whereas response.text decodes with a guess that may garble the document. The sitemaps protocol requires its namespace on every sitemap, which is what lets you pull every URL from a sitemap at crawl time. Real-world fetches fail in predictable ways: gzipped responses, wrong encodings, HTTP 403 and 429 errors, and anti-bot systems that block plain requests clients. Before aiming a script at a server you do not control, limit it to public pages that need no login, and review that site's robots.txt and terms of service first.

Parse XML safely: prevent XXE and billion-laughs attacks

The standard-library XML parsers — ElementTree, minidom, and SAX — offer no protection against hostile documents: a booby-trapped file can pull in local files via an XML External Entity (XXE) attack, or drain memory with a billion-laughs entity expansion. The Python documentation's XML vulnerabilities page says as much directly. For XML from any untrusted source, parse with defusedxml, which is a drop-in replacement. Treat it as a hard rule: if the source is untrusted, parse with defusedxml. That covers anything downloaded from the web, uploaded by a user, or delivered by a third-party system. A config file produced by your own build pipeline is safe to read with the standard library; a feed returned by a stranger's server is not. Since switching costs a single import line, there is little excuse for skipping it.

Handle ParseError and malformed XML

Real-world XMLs are messy and can break, so wrap parsing in try/except and catch xml.etree.ElementTree.ParseError. The usual causes include unclosed tags, a wrong encoding declaration, and stray ampersands that should be &. With recover=True, lxml attempts to fix what it can rather than rejecting the document outright, which rescues most truncated or carelessly generated feeds. Keep the defensive habit from the attributes section too: find() yields None for a missing element, so test for None before reading .text on data you do not control.

Python XML library comparison
LibraryInstallBest forXPath supportLarge filesNotes
ElementTreeBuilt-inDefault everyday parsingPartial (subset)OK via iterparse()Start here
lxmlpip install lxmlSpeed plus full XPathFull XPath 1.0ExcellentNear drop-in for ElementTree
xmltodictpip install xmltodictXML to dict and JSONNonePoorOne-line conversion
xml.dom.minidomBuilt-inSmall docs, pretty-printingNonePoorDOM API, verbose
xml.saxBuilt-inEvent streaming, low memoryNoneExcellentCallback-based, more code
BeautifulSoup (lxml-xml)pip install beautifulsoup4 lxmlHandling messy markupNone (CSS-style finds)OKSlowest of the group

Library capabilities as described in the captured guide; check current documentation for your installed versions.

Frequently asked questions

What Python package is used to parse XML?

Python offers more than one XML parsing option, and the standard library's xml.etree.ElementTree covers the majority of routine work with nothing to install. Beyond that baseline, lxml is the choice for full XPath and higher throughput, xmltodict is the choice when the goal is a dictionary, and defusedxml is the choice whenever the input cannot be trusted.

How do you parse an XML file in Python?

With the standard library, call ET.parse("file.xml"), then .getroot() to get the root element, and query it with find(), findall(), or iter(). If your XML is already in a string, use ET.fromstring(), which returns the root element directly.

How do I convert XML to JSON in Python?

The usual route is two steps: xmltodict.parse() turns the XML into a Python dictionary, and json.dumps() serializes that dictionary as JSON. Two conventions matter in the resulting structure — attribute names become keys prefixed with @, and any element text that coexists with attributes is placed under the #text key.

Why does findall() return an empty list?

In most cases the document uses XML namespaces. ElementTree rewrites each tag into the {namespace-uri}tag form, so unqualified names match nothing; supply a namespace map such as root.findall("sm:url", {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}).

Is it safe to parse XML from an untrusted source?

Treat standard-library parsing as unsafe for hostile input: a maliciously built document can exploit XXE to reach local files, or trigger billion-laughs entity expansion to consume memory. The remedy is defusedxml, which substitutes for the standard parser with no meaningful code changes.

Should I use ElementTree or lxml?

Reach for ElementTree first: it is bundled with Python and handles routine parsing without extra dependencies. Move to lxml only when a concrete need appears — the full XPath 1.0 expression language, noticeably quicker parsing, or iterparse() streaming that keeps memory flat while processing very large documents.