import sys

import re



def glue_xml_lines(xml):
    new_xml = xml[0]
    previous_line_ended_in_newline = new_xml.endswith(b'\n')
    previous_line_was_indentet = False
    for line in xml[1:]:
        if (re.match(b'^[ \t]+', line) and previous_line_ended_in_newline):
            new_xml = b''.join((new_xml, line))
            previous_line_was_indentet = True
        elif previous_line_was_indentet:
            new_xml = b''.join((new_xml, line))
            previous_line_was_indentet = False
        else:
            new_xml = b'\n'.join((new_xml, line))
            previous_line_was_indentet = False

        previous_line_ended_in_newline = line.endswith(b'\n')

    return new_xml


def read_xml_file(filename):
    xml = []

    with open(filename, 'rb') as fd:
        for line in fd:
            # These explode etree and are useless
                continue
            xml.append(line)

    return glue_xml_lines(xml)


for filename in sys.argv:
    result = read_xml_file(filename)
    output = open(filename+".new", 'wb')
    output.write(result)
    output.close()

Source