May 30, 2018
2 mins read
XML Pretty Print using Python is required frequently for testing, analyzing and debugging XML data. Python and XML both are treading in programming fields. Python generates dynamic XML string and received by the client. To save memory or bandwidth it’s better to send the minified version of XML text.
Some time for debugging purposes, we need to see beautified XML data to understand and to Debug. This article will help to pretty-print XML data.
There are two examples in the post.
Here is an example:
|
|
Result :
|
|
Here is the explanation for the code.
import xml.dom.minidom
This is an XML library available in python to convert the DOM object from XML string or from the XML file.
xml.dom.minidom.parseString
parseString
is a method which converts XML string to objectxml.dom.minidom.Document
.
xml.toprettyxml
toprettyxml
This method will indent the XML data and return as Pretty XML string.
Here is the Example:
|
|
This program reads from file and load and create XML Document objects and print the XML data in beautiful XML String.
I have used these tools for creating this blog post.
Related articles:
Sharing is caring!