How to Format XML in Python
Complete guide to pretty-printing and formatting XML in Python using the built-in xml.dom.minidom module and the lxml library.
Format XML with xml.dom.minidom (built-in)
Python's standard library includes xml.dom.minidom — no installation needed.
The toprettyxml() method formats parsed XML with indentation.
import xml.dom.minidom
xml_string = '<root><name>Alice</name><age>30</age></root>'
dom = xml.dom.minidom.parseString(xml_string)
formatted = dom.toprettyxml(indent=' ')
print(formatted)
Remove the Extra XML Declaration Line
toprettyxml() adds <?xml version="1.0" ?> at the top. To strip it:
import xml.dom.minidom
dom = xml.dom.minidom.parseString(xml_string)
lines = dom.toprettyxml(indent=' ').split('\n')
# Skip the first line (XML declaration)
formatted = '\n'.join(lines[1:])
print(formatted)
Format an XML File
import xml.dom.minidom
# Read and reformat an XML file
with open('data.xml', 'r') as f:
xml_string = f.read()
dom = xml.dom.minidom.parseString(xml_string)
formatted = dom.toprettyxml(indent=' ')
with open('data_formatted.xml', 'w') as f:
f.write(formatted)
Format XML with lxml (third-party)
lxml is faster and more feature-rich than minidom. Install with pip install lxml.
from lxml import etree
xml_string = b'<root><name>Alice</name><age>30</age></root>'
root = etree.fromstring(xml_string)
etree.indent(root, space=' ')
formatted = etree.tostring(root, pretty_print=True).decode()
print(formatted)
Format XML from Command Line (Python one-liner)
# Format XML file via command line
python3 -c "
import xml.dom.minidom, sys
print(xml.dom.minidom.parse(sys.argv[1]).toprettyxml(indent=' '))
" data.xml
Need to format XML without writing code?
Paste any XML into the free online formatter — instant results, no Python environment needed.
Open XML Formatter →Frequently Asked Questions
How do I pretty print XML in Python?
Use xml.dom.minidom.parseString(xml_string).toprettyxml(indent=' '). This uses Python's built-in library and requires no extra installation.
Why does toprettyxml add blank lines?
minidom adds extra newlines between text nodes. Filter them out with: '\n'.join([l for l in output.split('\n') if l.strip()])
minidom vs lxml — which should I use?
Use minidom for simple scripts with no dependencies. Use lxml for production code, large files, or when you need XPath, XSLT, or schema validation support.