Home >Backend Development >Python Tutorial >How Can I Pretty Print XML in Python?
Pretty Printing XML in Python
In Python, there are several approaches to pretty print XML, which involves formatting the XML content for clarity and readability. One of the most straightforward methods is to use the xml.dom.minidom module.
To pretty print XML using xml.dom.minidom, follow these steps:
Parse the XML:
Create a Pretty Printed XML String:
import xml.dom.minidom dom = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string) pretty_xml_as_string = dom.toprettyxml()
The pretty_xml_as_string variable now contains the formatted XML as a string. You can use this string for further processing or display.
The above is the detailed content of How Can I Pretty Print XML in Python?. For more information, please follow other related articles on the PHP Chinese website!