pretty print xml in python

To pretty print XML in Python, you can use the minidom module which is part of the standard library.

Here is an example code snippet to pretty print XML in Python using minidom:

main.py
import xml.dom.minidom

# Define the XML string
xml_string = "<root><example><name>John</name><age>32</age></example><example><name>Jane</name><age>28</age></example></root>"

# Parse the XML string
dom = xml.dom.minidom.parseString(xml_string)

# Pretty print the XML data
pretty_xml_string = dom.toprettyxml()

# Print the pretty XML string
print(pretty_xml_string)
368 chars
14 lines

In this example, we define an XML string, parse it using minidom, and then pretty print the resulting data using the toprettyxml() method of the dom object. Finally, we print the pretty XML string.

This should produce a nicely formatted XML string that is easier to read and understand.

gistlibby LogSnag