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's an easy way of putting it back in whether through lxml or some other solution?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can also preserve DOCTYPE and the XML declaration with
fromstring()
:Output
Note the xml declaration (with correct encoding) and doctype are present. It even (possibly incorrectly) uses
'
instead of"
in the xml declaration and addsContent-Type
to the<head>
.For the @John Keyes' example input it produces the same results as
etree.tostring()
in the answer.The following will include the DOCTYPE and the XML declaration:
Note,
tostring
does not preserve theDOCTYPE
if you create anElement
(e.g. usingfromstring
), it only works when you process the XML usingparse
.Update: as pointed out by J.F. Sebastian my assertion about
fromstring
is not true.Here is some code to highlight the differences between
Element
andElementTree
serialization:and the output is: