What is the best way (or are the various ways) to pretty print XML in Python?
If your xml exists as a minidom node, you can use the toprettyxml() function. If it really only ever exists as a string, you will have to parse it in before you can pretty print it out.
Browse for the XML file by clicking File->Open or pressing Ctrl+O. Click File->Print or press Ctrl+P to open the Printer window.
import xml.dom.minidom dom = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string) pretty_xml_as_string = dom.toprettyxml()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With