LXML kills my CDATA sections

I batch convert many XML files, changing my character encodings to UTF-8:

with open(source_filename, "rb") as source: tree = etree.parse(source) with open(destination_filename, "wb") as destination: tree.write(destination, encoding="UTF-8", xml_declaration=True) 

Unfortunately, it destroys my CDATA partitions and just eludes them.

A source

 <d><![CDATA[áÌÀøÅàùÑÄéú ëÌÄé áÈàÅùÑ éäå''ä ðÄùÑÀôÌÈè <small><small>(ùí ëå èæ)</small></small> 

Destination

 <d>בְּרֵאשִׁית כִּי בָאֵשׁ יהו''ה נִשְׁפָּט &lt;small&gt;&lt;small&gt;(שם כו טז)&lt;/small&gt;&lt;/small&gt; 

Is there a setting I can set that tells me that it will leave my CDATA sections alone? I mainly use LXML to change the character encoding and to spell the XML header correctly.

+5
source share
1 answer

Use the strip_cdata=False parameter:

 import lxml.etree as etree parser = etree.XMLParser(strip_cdata=False) with open(source_filename, "rb") as source: tree = etree.parse(source, parser=parser) 
+11
source

Source: https://habr.com/ru/post/1202439/


All Articles