Skip to content

Instantly share code, notes, and snippets.

@t2y
Created December 3, 2012 01:04
Show Gist options
  • Save t2y/4191973 to your computer and use it in GitHub Desktop.
Save t2y/4191973 to your computer and use it in GitHub Desktop.
python 1 liner for xml pretty print with standard input
$ cat sample.xml | python -c "import sys; from xml.dom.minidom import parse; print parse(sys.stdin).toprettyxml(indent=' ')"
@pieper
Copy link

pieper commented Nov 21, 2022

or for python3:

python -c "import sys; from xml.dom.minidom import parse; print(parse(sys.stdin).toprettyxml(indent=' '))"

@major1201
Copy link

To avoid empty lines:

python3 -c "import sys; from xml.dom.minidom import parse; print('\n'.join([line for line in parse(sys.stdin).toprettyxml().split('\n') if line.strip()]))"

ref: https://stackoverflow.com/questions/14479656/empty-lines-while-using-minidom-toprettyxml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment