from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
foo = """<node1>
    <node2>
        <node3>foo  </node3>
    </node2>
</node1>"""
bar = etree.XML(foo, parser)
print(etree.tostring(bar, pretty_print=False, with_tail=True))
Crunching xml with python