result = etree.tostring(fromstring_tree, xml_declaration=True, encoding=
    encoding)
print("""%s
fromstring ElementTree:
%s
""" % ('-' * 20, result))
fromstring_element = etree.fromstring(xml_str)
result = etree.tostring(fromstring_element, xml_declaration=True)
print("""%s
fromstring Element:
%s
""" % ('-' * 20, result))
Preserving original doctype and declaration of an lxml.etree parsed xml