Last active
August 24, 2020 11:40
-
-
Save eliask/d8517790b11edac75983d1e6fdab3cab to your computer and use it in GitHub Desktop.
Pretty print XML files with python xml.dom.minidom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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