Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.5 - updating of XML and keep <!DOCTYPE>

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?

like image 912
Erik Šťastný Avatar asked Sep 14 '25 14:09

Erik Šťastný


1 Answers

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.

like image 119
Rody Oldenhuis Avatar answered Sep 16 '25 02:09

Rody Oldenhuis