Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty printing XML in Python

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

like image 805
Hortitude Avatar asked Apr 15 '09 00:04

Hortitude


People also ask

How do I print a pretty XML string in Python?

If your xml exists as a minidom node, you can use the toprettyxml() function. If it really only ever exists as a string, you will have to parse it in before you can pretty print it out.

How do I print an XML file?

Browse for the XML file by clicking File->Open or pressing Ctrl+O. Click File->Print or press Ctrl+P to open the Printer window.


1 Answers

import xml.dom.minidom  dom = xml.dom.minidom.parse(xml_fname) # or xml.dom.minidom.parseString(xml_string) pretty_xml_as_string = dom.toprettyxml() 
like image 179
Ben Noland Avatar answered Sep 28 '22 07:09

Ben Noland