def METHOD_NAME(elem, level=0):
    i = "\n" + level * "  "
    j = "\n" + (level + 1) * "  "
    if len(elem):
        elem.text = j
        n = 1
        for subelem in elem:
            if n != len(elem):
                METHOD_NAME(subelem, level + 1)
                subelem.tail = j
            else:
                subelem.tail = i
            n += 1
    else:
        elem.tail = i
    return elem