# Parse XML in Python: ElementTree, lxml, Tested

> Parse XML in Python five ways on a real sitemap: ElementTree, lxml, Beautiful Soup, pandas.read_xml and xmltodict, with namespaces, timings and RSS.

[Home](https://quanticdata.io/) / [Blog](https://quanticdata.io/blog/) / Parse XML in Python: ElementTree, lxml, Tested

# Parse XML in Python: ElementTree, lxml, Beautiful Soup, pandas and xmltodict on one sitemap

Guides · Nov 20, 2026 · 5 min read · By [Aldo Morese](https://quanticdata.io/about/), founder of QuanticData

![Parsing an XML sitemap of 2,618 URLs in Python: ElementTree with the sitemap namespace finds all of them where findall without the namespace finds 0, and the card compares parse times of 3.8 ms for lxml, 6.9 ms for ElementTree, 18.8 ms for xmltodict and 102.6 ms for Beautiful Soup.](https://quanticdata.io/blog/parse-xml-python/hero.webp?v=20261120h)

On this page

- [Step by step with ElementTree](https://quanticdata.io/blog/parse-xml-python/#step-by-step-with-elementtree)
- [The namespace trap](https://quanticdata.io/blog/parse-xml-python/#the-namespace-trap)
- [Five parsers on the same file](https://quanticdata.io/blog/parse-xml-python/#five-parsers-on-the-same-file)
- [XML to a dictionary](https://quanticdata.io/blog/parse-xml-python/#xml-to-a-dictionary)
- [Parse an RSS feed](https://quanticdata.io/blog/parse-xml-python/#parse-an-rss-feed)
- [Attributes, missing values and pretty printing](https://quanticdata.io/blog/parse-xml-python/#attributes-missing-values-and-pretty-printing)
- [Large files: iterparse](https://quanticdata.io/blog/parse-xml-python/#large-files-iterparse)
- [XML from websites: where proxies come in](https://quanticdata.io/blog/parse-xml-python/#xml-from-websites-where-proxies-come-in)

To parse XML in Python, use the standard library: `import xml.etree.ElementTree as ET`, `root = ET.fromstring(data)` for a string or bytes (or `ET.parse("file.xml").getroot()` for a file), then `root.findall()`, `.find()`, `.text` and `.get()`. When the document declares a namespace, pass it in a dictionary or nothing will match.

**Run it on the free credit.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-lead&utm_content=parse-xml-python) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

We tested five ways on 20 November 2026 with Python 3.12.10, on a real XML document: our own sitemap, quanticdata.io/sitemap.xml, 313,387 bytes and 2,618 URLs, downloaded through a QuanticData Residential Premium proxy. Then an RSS feed, the other XML you meet most when scraping.

## Step by step with ElementTree

```python
import requests, xml.etree.ElementTree as ET

PROXY = "http://USER-country-gb-sid-x01-ttl-30m:PASS@residentialpremium.quanticdata.io:8080"
r = requests.get("https://quanticdata.io/sitemap.xml",
                 proxies={"http": PROXY, "https": PROXY}, timeout=30)
root = ET.fromstring(r.content)
print(root.tag)
# {http://www.sitemaps.org/schemas/sitemap/0.9}urlset

NS = {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}
print(len(root.findall("url")))          # 0
locs = [u.findtext("sm:loc", namespaces=NS) for u in root.findall("sm:url", NS)]
print(len(locs), locs[0])                # 2618 https://quanticdata.io/
```

1. **Parse bytes.** Pass `r.content`: the XML declaration names its own encoding.
2. **Look at the root tag.** The part in braces is the namespace. Here every element lives in it.
3. **Map a prefix to it** and use the prefix in every path: `sm:url`, `sm:loc`.
4. **Read values** with `findtext()`, which returns None instead of failing when a child is missing: 2,512 of the 2,618 URLs had a lastmod.

## The namespace trap

`root.findall("url")` returned 0 on a file with 2,618 url elements. No error, just nothing. Sitemaps, RSS with extensions, Atom, SOAP and office files all declare namespaces. Three ways to handle it in ElementTree, each of which found all 2,618 in our run:

- A prefix map, as above: `findall("sm:url", NS)`.
- The full name in braces: `findall("{http://www.sitemaps.org/schemas/sitemap/0.9}url")`.
- A wildcard: `findall("{*}url")` matches the name in any namespace.

## Five parsers on the same file

| Library | Code | Result | Median parse + extract |
| --- | --- | --- | --- |
| lxml | `etree.fromstring(b).xpath("//sm:loc/text()", namespaces=NS)` | 2,618 | 3.8 ms |
| ElementTree (standard library) | `ET.fromstring(b).findall("sm:url", NS)` | 2,618 | 6.9 ms |
| xmltodict | `xmltodict.parse(b)["urlset"]["url"]` | 2,618 dicts | 18.8 ms |
| Beautiful Soup | `BeautifulSoup(b, "xml").find_all("loc")` | 2,618 | 102.6 ms |
| pandas | `pd.read_xml(io.BytesIO(b))` | DataFrame (2618, 6) |  |

Medians of 10 runs each on this PC. lxml was fastest and adds XPath with conditions, for example `//sm:loc[contains(., '/blog/')]/text()`, which returned the 297 blog URLs; more in [lxml XPath in Python](https://quanticdata.io/blog/lxml-xpath-python/). Beautiful Soup ignores namespaces by name (`find_all("loc")` just works) and needs lxml installed for its "xml" parser. pandas returned one row per url with the columns loc, lastmod, changefreq, priority, link and video, ready for analysis, with lastmod as text (dtype str in our run) until you convert it with `pd.to_datetime`, which turned it into datetime64; its HTML twin is [pandas read_html](https://quanticdata.io/blog/pandas-read-html/).

## XML to a dictionary

The autocomplete asks for "parse xml python to dict" more than anything else. xmltodict (`pip install xmltodict`) does it in one call:

```python
import xmltodict
d = xmltodict.parse(r.content)
print(type(d["urlset"]["url"]).__name__, len(d["urlset"]["url"]))   # list 2618
print(d["urlset"]["url"][0])
# {'loc': 'https://quanticdata.io/', 'lastmod': '2026-10-19'}
```

One catch: an element that appears once becomes a dictionary, and the same element twice becomes a list. Code that reads `["url"]` must handle both, or pass `force_list=("url",)`: on a one-url test file the value was a dict without it and a list with it. From the dictionary, `json.dumps(d)` gives XML to JSON; see [parsing JSON in Python](https://quanticdata.io/blog/parse-json-python/).

**Try it on your own targets.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-inline&utm_content=parse-xml-python) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

## Parse an RSS feed

RSS 2.0 usually has no namespace on its main elements, so plain paths work:

```
f = requests.get("https://quanticdata.io/blog/feed.xml", proxies={"http": PROXY, "https": PROXY}, timeout=30)
channel = ET.fromstring(f.content).find("channel")
items = channel.findall("item")
print(len(items), items[0].findtext("title"), items[0].findtext("pubDate"))
# 153 Residential Proxy Success Rate on 20 Websites Wed, 18 Nov 2026 18:42:45 GMT
```

## Attributes, missing values and pretty printing

Values often sit in attributes rather than text. A small catalogue shows the calls:

```
s = '<catalog><book id="b1" lang="en"><title>A Light in the Attic</title>' \
    '<price currency="GBP">51.77</price></book></catalog>'
root = ET.fromstring(s)
book = root.find("book")
print(book.get("id"), book.attrib)          # b1 {'id': 'b1', 'lang': 'en'}
print(root.find("book/price").get("currency"), float(root.findtext("book/price")))  # GBP 51.77
print(book.get("isbn", "n/a"))              # n/a

new = ET.SubElement(root, "book", id="b2")
ET.SubElement(new, "title").text = "Tipping the Velvet"
ET.indent(root)
print(ET.tostring(root, encoding="unicode"))
```

`.get()` reads one attribute and takes a default for missing ones, `.attrib` gives them all as a dictionary, and text always comes back as a string, so convert numbers yourself. `ET.indent()` pretty-prints the tree before `tostring()`: our output came back indented two spaces per level, with the new book appended at the end.

## Large files: iterparse

`fromstring` holds the whole tree in memory. For files of hundreds of megabytes, stream them and clear each element after use:

```
n = 0
for event, el in ET.iterparse("sitemap.xml", events=("end",)):
    if el.tag == "{http://www.sitemaps.org/schemas/sitemap/0.9}url":
        n += 1
        el.clear()
print(n)   # 2618
```

## XML from websites: where proxies come in

Sitemaps and feeds are how scrapers discover pages: read the sitemap, then fetch each URL (the crawl itself is in [how to web crawl with Python](https://quanticdata.io/blog/how-to-web-crawl-python/)). Keep the request rate modest when you fetch the URLs a sitemap lists. We fetched these files through a UK Residential Premium session; QuanticData [residential proxies](https://quanticdata.io/residential-proxies/) take the country and session in the username, and [mobile proxies](https://quanticdata.io/mobile-proxies/) put requests on carrier IPs. Try them with the [Residential Basic free trial](https://quanticdata.io/residential-proxy-free-trial/).

Parsing XML from untrusted sources has its own risk: Python's documentation warns about malicious XML such as entity expansion bombs. For files you do not control, the same documentation points to the `defusedxml` package.

### Sources & further reading

- [Python docs: xml.etree.ElementTree](https://docs.python.org/3/library/xml.etree.elementtree.html)
- [Python docs: XML vulnerabilities](https://docs.python.org/3/library/xml.html#xml-vulnerabilities)
- [lxml: XPath and XSLT](https://lxml.de/xpathxslt.html)
- [pandas.read_xml](https://pandas.pydata.org/docs/reference/api/pandas.read_xml.html)
- [sitemaps.org protocol](https://www.sitemaps.org/protocol.html)

## FAQ

Quick answers on parse xml python.

[Something else? Ask us](mailto:hello@quanticdata.io)

### How do I parse XML in Python?

Use `xml.etree.ElementTree`: `root = ET.fromstring(data)` or `ET.parse("file.xml").getroot()`, then `findall()`, `find()`, `.text` and `.get("attr")`. Pass a namespace map when the root tag shows a namespace in braces.

### Why does ElementTree findall return nothing?

Almost always a namespace. On our sitemap, `findall("url")` returned 0 and `findall("sm:url", NS)` returned 2,618. Use a prefix map, the full `{uri}name`, or `{*}name`.

### Which is faster, lxml or ElementTree?

lxml. On a 2,618-URL sitemap it took 3.8 ms against 6.9 ms for ElementTree, 18.8 ms for xmltodict and 102.6 ms for Beautiful Soup. ElementTree needs no install.

### How do I convert XML to a dictionary in Python?

Install xmltodict and call `xmltodict.parse(data)`. Repeated elements become lists and single ones dictionaries; use `force_list` to make them consistent. `json.dumps()` on the result gives JSON.

### How do I read XML into pandas?

`pd.read_xml(io.BytesIO(data))`. On our sitemap it returned a DataFrame of 2,618 rows and 6 columns, one row per url element.

## Fetch sitemaps and feeds from the IP you choose

Residential and mobile proxies with country and session settings in the username, for requests, httpx and Scrapy.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-end&utm_content=parse-xml-python) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

## Collectors with Python examples

- [Reddit scraper API in Python](https://quanticdata.io/collectors/reddit-scraper-api/python/): Collector · Install, first call, async runs, output fields and errors of the Reddit scraper collector, with Python examples.
- [YouTube channel scraper API in Python](https://quanticdata.io/collectors/youtube-channel-scraper-api/python/): Collector · Install, first call, async runs, output fields and errors of the YouTube channel scraper collector, with Python examples.
- [Amazon product scraper API in Python](https://quanticdata.io/collectors/amazon-product-api/python/): Collector · Install, first call, async runs, output fields and errors of the Amazon product scraper collector, with Python examples.
- [Flipkart Scraper API in Python](https://quanticdata.io/collectors/flipkart-scraper-api/python/): Collector · Install, first call, async runs, output fields and errors of the Flipkart Scraper collector, with Python examples.

## Related reading

- [urllib3 vs requests: Speed and Bandwidth](https://quanticdata.io/blog/urllib3-vs-requests/): Guides · requests is built on urllib3 and adds sessions, JSON, exceptions on errors and compression by default. urllib3 alone was faster on a local server, the same through a proxy, and downloaded 9.5 times more bytes until we asked for compression. Measured, with the code.
- [Python requests POST: JSON, Form Data, Files](https://quanticdata.io/blog/python-requests-post/): Guides · requests.post(url, json=payload) sends a JSON body with the right Content-Type; data= sends form fields; files= uploads. We posted each one through a residential proxy to a test server and print what the server received, including the redirect that silently turns a POST into a GET.
- [Concurrency vs Parallelism, Measured in Python](https://quanticdata.io/blog/concurrency-vs-parallelism/): Guides · Concurrency is making progress on several tasks by switching between them; parallelism is running several at the same instant on different CPU cores. We timed both on a real scraping job in Python on 20 November 2026: threads cut 30 downloads from 10.3 s to 3.5 s, and only processes cut the parsing, from 3.5 s to 0.8 s.

---

Source: https://quanticdata.io/blog/parse-xml-python/ · Site index for AI: https://quanticdata.io/llms.txt
