I\'m using python\'s lxml and I\'m trying to read an xml document, modify and write it back but the original doctype and xml declaration disappears. I\'m wondering if there\
You can also preserve DOCTYPE and the XML declaration with fromstring()
:
import sys
from StringIO import StringIO
from lxml import etree
xml = r'''
example
This is an example
'''
tree = etree.fromstring(xml).getroottree() # or etree.parse(file)
tree.write(sys.stdout, xml_declaration=True, encoding=tree.docinfo.encoding)
example
This is an example
Note the xml declaration (with correct encoding) and doctype are present. It even (possibly incorrectly) uses '
instead of "
in the xml declaration and adds Content-Type
to the .
For the @John Keyes' example input it produces the same results as etree.tostring()
in the answer.