I have created a basic xml tree using lxml tutorial:
from lxml import etree
root = etree.Element("root")
root.append( etree.Element("child1") )
child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")
print(etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True))
This produces the following:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<child1/>
<child2/>
<child3/>
</root>
My question is, how to produce xml file with double quoted file header, i.e.
<?xml version="1.0" encoding="UTF-8"?>
....
lxml is a reference to the XML toolkit in a pythonic way which is internally being bound with two specific libraries of C language, libxml2, and libxslt. lxml is unique in a way that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API.
To add the header without concatenate manually you need to use the "doctype" parameter in tostring method as bellow:
with open(output_file, 'wb') as o:
o.write(etree.tostring(
document_root, pretty_print=True,
doctype='<?xml version="1.0" encoding="ISO-8859-1"?>'
))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With