Skip to content

Instantly share code, notes, and snippets.

@jiahut
Last active November 7, 2018 09:24
Show Gist options
  • Select an option

  • Save jiahut/e532f9d5c6a4a4b88fb234f874c19770 to your computer and use it in GitHub Desktop.

Select an option

Save jiahut/e532f9d5c6a4a4b88fb234f874c19770 to your computer and use it in GitHub Desktop.
#/usr/bin/env python
import re
import sys
from xml.dom.minidom import parseString
_xml_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)
def pretty_xml_old(xml_str, indent=" "):
xml_re = _xml_re
# avoid re-prettifying large amounts of xml that is fine
if xml_str.count("\n") < 20:
pxml = parseString(xml_str).toprettyxml(indent)
return xml_re.sub('>\g<1></', pxml)
else:
return xml_str
if __name__ == '__main__':
print(pretty_xml_old(sys.stdin.read()))
@jiahut
Copy link
Author

jiahut commented Nov 7, 2018

cat test.xml | python -m xmltool

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