Friday, September 16, 2005

Xml prettifier in python

Since xml is the "format of the day" for any and everything, I find it more and more common to edit/generate/hack xml files in daily routine. Since xml is not exactly write/read friendly to humans, I end up writing tools to update and maintain these monsters :). Python has a good and friendly xml parsing api that I find useful. For starts, a prettifier can be written in a few lines of code:
import sys, StringIO
from xml.dom import minidom

def prettify(inxml):
xmldoc = minidom.parseString(inxml)
# use 2 spaces to indent instead of tabs
lines = StringIO.StringIO(xmldoc.toprettyxml(" "))
#remove redundant empty lines
lines = [ x for x in lines if x.strip() != ""]
return "".join(lines)

1 comment:

Anonymous said...

Thanks,
but it doesn't work when XML contains non-ASCII symbols