1# Use lxml as the core library for xml manipulation.
2from lxml import etree
3
4xml_object = etree.tostring(root,
5 pretty_print=True,
6 xml_declaration=True,
7 encoding='UTF-8')
8
9with open("xmlfile.xml", "wb") as writter: # wb - write bytes mode
10 writter.write(xml_object)`
11