Pretty printing XML in Python

Hortitude picture Hortitude · Apr 15, 2009 · Viewed 392.4k times · Source

What is the best way (or are the various ways) to pretty print XML in Python?

Answer

Ben Noland picture Ben Noland · Jul 30, 2009
import xml.dom.minidom

dom = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string)
pretty_xml_as_string = dom.toprettyxml()