I have XML starts with following:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE       ispXCF  SYSTEM  "IspXCF.dtd" >
<ispXCF version="3.7.0">
    <Comment></Comment>
    <Chain>
        <Comm>JTAG</Comm>
        <Device>
        ....
And I am using xml.etree.ElementTree parser. But this parser deletes the second line starting with <!DOCTYPE
I am using the following arguments in the write method:
tree.write("data.xml", encoding="utf-8", xml_declaration=True, short_empty_elements=None)
but it only keeps <?xml version='1.0' encoding='utf-8'?>
Is there some way how to not delete <!DOCTYPE line? Or will I have to use some other XML parser?
It seems xml.etree.ElementTree has very poor support for doctype declarations.
You can create them for new XML with TreeBuilder objects, but not read them from Element or ElementTree objects created from existing XML. 
As answered here, you'd have to manually copy paste the doctype declaration...pretty fugly IMHO.
So, in all, it would seem best to switch to lxml.
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