Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 11, 2015 15:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Averroes/6375a1cccd39fe9f2dd7 to your computer and use it in GitHub Desktop.
Save Averroes/6375a1cccd39fe9f2dd7 to your computer and use it in GitHub Desktop.
ElementTree pretty
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
#
"""Pretty-print function for ElementTree
"""
#end_pymotw_header
from xml.etree import ElementTree
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment