Welcome, guest | Sign In | My Account | Store | Cart

One-liner to pretty print an XML file using xml.dom.minidom.

Python, 11 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/usr/bin/env python
import xml.dom.minidom as md
import sys

pretty_print = lambda f: '\n'.join([line for line in md.parse(open(f)).toprettyxml(indent=' '*2).split('\n') if line.strip()])

if __name__ == "__main__":
   if len(sys.argv)>=2:
      print pretty_print(sys.argv[1])
   else:
      sys.exit("Usage: %s [xmlfile]" % sys.argv[0])

The "toprettyxml" function of xml.dom.minidom.Document object is pretty useful, but it produces rather unpretty XML with default values and adds un-necessary newlines. The "pretty_print" function is a one-liner that produces clean-looking XML using this function, indenting by just 2 spaces by default and removing the junk new-lines.

3 comments

ccpizza 12 years, 7 months ago  # | flag

I have used this code for prettyprinting, but it has the side-effect of removing all comments:

from xml.dom.ext import PrettyPrint
from xml.dom.ext.reader.Sax import FromXmlFile
import sys
doc = FromXmlFile(sys.argv[1])
PrettyPrint(doc, sys.stdout)
Ash 12 years ago  # | flag

The comments may work, but "xml.dom.ext" seems to be part of 4dom which isn't in any of my standard python installations (neither in 2.6 nor 3.0)