One-liner to pretty print an XML file using xml.dom.minidom.
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.
I have used this code for prettyprinting, but it has the side-effect of removing all comments:
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)
more recipes: http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python