XML header with lxml

Stefan Behnel stefan_ml at behnel.de
Tue Apr 5 04:16:13 EDT 2011

Jabba Laci, 04.04.2011 18:54:
> I want to construct an XML file with lxml but I don't find how to add
> the '<?xml version="1.0"?>' header.

This is not required. According to the XML spec, the default is:

   <?xml version="1.0" encoding="utf-8" standalone="false"?>

So, unless you diverge from these values, you do not need an XML 
declaration in your file. If you want to output the declaration anyway, you 
can simply prepend it to the string or write it into the output file before 

> from lxml import etree as ET
> html = ET.Element("html")
> print ET.tostring(html)
> simply prints
> <html/>

I imagine you are aware that HTML isn't XML.


More information about the Python-list mailing list