Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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

1 Answer

0 votes
by (106k 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))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...