ActiveState Code

Recipe 576750: Pretty-print XML


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

Python
 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])

Discussion

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.

Comments

  1. 1. At 4:18 a.m. on 17 jun 2009, ccpizza said:

    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)

  2. 2. At 4:20 a.m. on 17 jun 2009, ccpizza said:

    oops, i meant this code:

    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)
    
  3. 3. At 2:31 a.m. on 6 jan 2010, Ash said:

    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)

Sign in to comment