xml = et.XML('<root><test/><a>whatever</a><end_test/></root>')
root = tree.getroot()
root[:] = xml
root.text, root.tail = xml.text, xml.tail
print(et.tostring(tree, xml_declaration=True, encoding='utf-8'))
Creating a doctype with lxml's etree