if len(elem):
    if not elem.text or not elem.text.strip():
        elem.text = i + '  '
if not elem.tail or not elem.tail.strip():
    elem.tail = i
for subelem in elem:
    indent(subelem, level + 1)
if not elem.tail or not elem.tail.strip():
    elem.tail = j
elif level and (not elem.tail or not elem.tail.strip()):
    elem.tail = j
return elem
root = ElementTree.parse('/tmp/xmlfile').getroot()
indent(root)
ElementTree.dump(root)
Pretty printing XML in Python