xml

The Python xml package provides tools for parsing and creating XML documents that you can use to store and transport structured data. It offers a variety of modules for different XML processing needs, such as DOM, SAX, and ElementTree.

Here’s a quick example:

Python
>>> import xml.etree.ElementTree as ET

>>> root = ET.fromstring("<data><item>Python</item></data>")
>>> root.tag
'data'

Key Features

  • Parses XML documents from strings, file, and other data sources
  • Supports both SAX and DOM parsing models
  • Allows creation and modification of XML documents
  • Provides an API through ElementTree for common XML tasks
  • Handles Unicode and namespaces in XML documents
  • Supports reading and writing XML in both compact and pretty-printed formats
  • Validates XML against DTDs or schemas—with additional modules
  • Integrates with other Python modules for data interchange

Frequently Used Classes and Functions

Object Type Description
xml.etree.ElementTree.ElementTree Class Represents the whole XML document
xml.etree.ElementTree.Element Class Represents an element in the XML document
xml.etree.ElementTree.parse() Function Parses an XML document from a file
xml.etree.ElementTree.fromstring() Function Parses an XML document from a string
xml.etree.ElementTree.SubElement() Function Creates a new subelement under a parent element
xml.etree.ElementTree.dump() Function Writes a formatted XML representation of an element or tree to standard output
xml.etree.ElementTree.tostring() Function Generates a string representation of an XML element

Examples

Create an XML document:

Python
>>> import xml.etree.ElementTree as ET

>>> root = ET.Element("data")
>>> item = ET.SubElement(root, "item")
>>> item.text = "Python"
>>> tree = ET.ElementTree(root)
>>> ET.dump(tree)
<data><item>Python</item></data>

Common Use Cases

  • Parsing XML configuration files
  • Creating XML files for data interchange
  • Modifying existing XML documents
  • Extracting specific data from XML documents
  • Validating XML files for correctness
  • Converting between XML and other formats (JSON, CSV, etc.)
  • Scraping and processing XML-based web data
  • Embedding XML in larger data processing pipelines

Real-World Example

Suppose you need to parse an XML configuration file and extract specific settings. Here’s how you could do it:

Python
>>> import xml.etree.ElementTree as ET

>>> xml_content = """
... <config>
...     <setting name="theme">dark</setting>
...     <setting name="language">en</setting>
... </config>
... """

>>> root = ET.fromstring(xml_content)
>>> settings = {child.attrib["name"]: child.text for child in root}
>>> settings
{'theme': 'dark', 'language': 'en'}

In this example, you parse an XML document and extract configuration settings into a dictionary, showcasing how to work with XML data using Python’s xml package.

Tutorial

A Roadmap to XML Parsers in Python

In this tutorial, you'll learn what XML parsers are available in Python and how to pick the right parsing model for your specific use case. You'll explore Python's built-in parsers as well as major third-party libraries.

intermediate


By Leodanis Pozo Ramos • Updated July 30, 2025