Fix Python – Pretty printing XML in Python

Question

Asked By – Hortitude

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

Now we will see solution for issue: Pretty printing XML in Python


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()

This question is answered By – Ben Noland

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0