Python XML libraries

Updated Nov 2026

Python XML libraries parse XML documents into trees or dictionaries and write XML back out. An incremental parser processes a large document in steps rather than waiting for the whole input. These libraries let apps read feeds, configuration files, and API responses from services that return XML.

Popularity

lxml has the most monthly downloads in this comparison, at 445.4M.

LibraryMonthly downloadsStarsLast releaseLicense
lxml
445.4M
3.1KSep 2026BSD-3-Clause
Beautiful Soup
320.8M
Not on GitHubJun 2026MIT
defusedxml
276.9M
559Mar 2021PSF-2.0
xmltodict
116.3M
5.8KFeb 2026MIT
xmlschema
7.6M
475Jun 2026MIT
xsdata
3.6M
457Oct 2026MIT
pydantic-xml
3.1M
257Oct 2026Unlicense
untangle
321.6K
628Sep 2026MIT
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Features compared

LibraryNative XSD validationNested dictionary outputNamespace URI matching
lxmlBuilt-inUnsupportedBuilt-in
Beautiful SoupUnsupportedUnsupportedVia plugin
defusedxmlUnsupportedUnsupportedBuilt-in
xmltodictUnsupportedBuilt-inBuilt-in
xmlschemaBuilt-inBuilt-inBuilt-in
xsdataUnsupportedBuilt-inBuilt-in
pydantic-xmlUnsupportedBuilt-inBuilt-in
untangleUnsupportedUnsupportedUnsupported
Built-inVia pluginNot supported

Libraries and code examples

Parses, edits, and serializes XML trees with the ElementTree API.

Downloads / mo
445.4M
Stars
3.1K
Last release
6.1.3
License
BSD-3-Clause

Strengths

  • Builds and edits XML trees. Create elements, modify the tree, and serialize XML through ElementTree APIs.
  • Parses large inputs incrementally. Process input in steps rather than waiting for the entire document.

Limitations

  • Streaming still builds a tree. Clear processed elements when large inputs should not remain in memory.

Quickstart

From tutorial.html
$ pip install lxml
from lxml import etree
root = etree.fromstring(b'<message>Hello</message>')
print(root.text)

lxml examples

From open-source projects on GitHub

Beautiful Soup

Navigates, searches, and modifies XML or HTML parse trees.

Downloads / mo
320.8M
Last release
4.15.0
License
MIT

Strengths

  • Navigates XML with CSS selectors. Select namespaced XML elements with familiar CSS selector syntax.
  • Uses one markup-navigation interface. Search and modify parse trees through the same Beautiful Soup interface.

Limitations

  • XML parsing requires lxml. Install lxml and request the XML parser explicitly for XML input.

Quickstart

From doc.md
$ pip install beautifulsoup4 lxml
from bs4 import BeautifulSoup
soup = BeautifulSoup('<message>Hello</message>', 'xml')
print(soup.message.get_text())

Beautiful Soup examples

From open-source projects on GitHub

Provides protected XML parsing functions compatible with standard-library parser objects.

Downloads / mo
276.9M
Stars
559
Last release
0.7.1
License
PSF-2.0

Strengths

  • Blocks entity declarations by default. The default parser settings reject declared entities and external references.
  • Keeps standard-library object compatibility. Use returned parser objects with compatible standard-library XML APIs.

Limitations

  • Writing XML uses standard-library APIs. Use standard-library serializers for tree creation and XML output.
  • DTD rejection needs explicit configuration. Set forbid_dtd=True when documents containing any DTD must be rejected.

Quickstart

From README.md
$ pip install defusedxml
from defusedxml.ElementTree import fromstring
root = fromstring('<message>Hello</message>')
print(root.text)

defusedxml examples

From open-source projects on GitHub

Converts XML documents to dictionaries and writes dictionaries as XML.

Downloads / mo
116.3M
Stars
5.8K
Last release
1.0.4
License
MIT

Strengths

  • Writes dictionaries back as XML. Use unparse to generate XML from a Python dictionary.
  • Streams records through callbacks. Process records with item_depth and item_callback for large XML inputs.

Limitations

  • Round-trips can change document structure. Mixed content order, attribute order, and comment placement are not fully preserved.
  • Namespaces need explicit processing. Enable process_namespaces when namespace URIs must be expanded.

Quickstart

From README.md
$ pip install xmltodict
import xmltodict
data = xmltodict.parse('<message>Hello</message>')
print(data['message'])

xmltodict examples

From open-source projects on GitHub

Validates and decodes XML Schema documents.

Downloads / mo
7.6M
Stars
475
Last release
4.3.2
License
MIT

Quickstart

From usage.html
$ pip install xmlschema
import xmlschema
schema = xmlschema.XMLSchema('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="message" type="xs:string"/></xs:schema>')
schema.validate('<message>Hello</message>')

xmlschema examples

From open-source projects on GitHub

Provides XML data binding for Python.

Downloads / mo
3.6M
Stars
457
Last release
26.10
License
MIT

Quickstart

From README.md
$ pip install xsdata
from dataclasses import dataclass, field
from xsdata.formats.dataclass.parsers import XmlParser

@dataclass
class Message:
    class Meta:
        name = "message"
    text: str = field(default="", metadata={"type": "Text"})

message = XmlParser().from_string("<message>Hello</message>", Message)
print(message.text)

xsdata examples

From open-source projects on GitHub

Extends Pydantic with XML support.

Downloads / mo
3.1M
Stars
257
Last release
2.22.0
License
Unlicense

Quickstart

From quickstart.html
$ pip install pydantic-xml
from pydantic_xml import BaseXmlModel, element

class Message(BaseXmlModel, tag="message"):
    text: str = element()

message = Message.from_xml(b"<message><text>Hello</text></message>")
print(message.text)

pydantic-xml examples

From open-source projects on GitHub

Converts XML to Python objects

Downloads / mo
321.6K
Stars
628
Last release
1.3.0
License
MIT

Quickstart

From README.md
$ pip install untangle
import untangle
message = untangle.parse('<message>Hello</message>')
print(message.message.cdata)

untangle examples

From open-source projects on GitHub

FAQ

lxml suits building, editing, and parsing XML trees with the ElementTree API. defusedxml suits untrusted XML that may declare entities. xmltodict suits code that works with XML as Python dictionaries.

lxml parses, edits, and serializes XML trees with the ElementTree API. It also parses large inputs incrementally.

xmltodict converts XML documents to dictionaries and writes dictionaries back as XML. Round-trips may not preserve mixed content order, attribute order, or comment placement.

defusedxml provides protected parsing functions that reject declared entities and external references by default.

lxml builds and edits XML trees directly. Beautiful Soup navigates XML with CSS selectors and needs lxml as its XML parser.

For coding agents

Found your XML library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official