0 votes
1 view
in Python by (47.8k points)

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

1 Answer

0 votes
by (107k points)

There are many ways to Pretty print XML in Python some of the important are as follows:-

import xml.dom.minidom

dom = xml.dom.minidom.parse(xml_fname)

pretty_xml_as_string = dom.toprettyxml()

Another thing you can use that lxml has recently, updated, that includes a pretty print function. Below is the code for the same.

import lxml.etree as etree

x = etree.parse("filename")

print(etree.tostring(x, pretty_print=True))

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...