This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Support CDATA by xml.etree.(c)ElementTree
Type: enhancement Stage: needs patch
Components: XML Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Pierre van de Laar, scoder
Priority: normal Keywords:

Created on 2019-05-10 09:51 by Pierre van de Laar, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg342067 - (view) Author: Pierre van de Laar (Pierre van de Laar) Date: 2019-05-10 09:51
I would like to add information to CDATA in an Xml Tree.
Turns out I am not the only one: https://stackoverflow.com/questions/174890/how-to-output-cdata-using-elementtree


Can the library be extended to also support CDATA (similar to Comment)?
Saves a lot of hacking...
msg342072 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-05-10 12:32
PR welcome. This is how lxml implements it:
https://lxml.de/api.html#cdata

Tests are here:
https://github.com/lxml/lxml/blob/1a2db33aa8b9619c1caf407167567d5cca0b9019/src/lxml/tests/test_etree.py#L1692-L1749

I guess it won't look perfectly the same in ElementTree in the end, but it might be enough to implement a string wrapper class (or even str subclass?), and then maybe special-case it in the serialiser (probably needed for the escaping). However, anything that relieves the serialiser from doing special work for this exceptional case might be a good idea.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81055
2019-05-10 12:32:09scodersetstage: needs patch
messages: + msg342072
versions: + Python 3.8
2019-05-10 10:03:53xtreaksetnosy: + scoder
2019-05-10 09:51:15Pierre van de Laarcreate