Skip to content

Instantly share code, notes, and snippets.

@eliask
Last active August 24, 2020 11:40
Show Gist options
  • Save eliask/d8517790b11edac75983d1e6fdab3cab to your computer and use it in GitHub Desktop.
Save eliask/d8517790b11edac75983d1e6fdab3cab to your computer and use it in GitHub Desktop.
Pretty print XML files with python xml.dom.minidom
#!/usr/bin/env python
# ppxml - pretty print XML from stdin or argv files
#
# NB: if multiple XML files are specifies, concatenates the prettyprinted
# versions one after the other.
#
# Python 2 and Python 3 compatible AFAICT.
#
import sys
import xml.dom.minidom as md
#INDENT = '\t'
INDENT = ' ' * 4
def prettyprint(fh):
print ('\n'.join(
line for line in
md.parse(fh).toprettyxml(indent=INDENT).split('\n')
if line.strip()
))
if sys.argv[1:]:
for path in sys.argv[1:]:
with open(path, 'rb') as fh:
prettyprint(fh)
else:
prettyprint(sys.stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment