Python – Pretty printing XML in Python

pretty-printpythonxml

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

Best Answer

import xml.dom.minidom

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