0

One of my subelements of my xml file has to contain a HTML code snippet eg.:

import import xml.etree.ElementTree as ET

.
.
.

qtext = ET.SubElement(questiontext, 'text')
qtext.text = '<![CDATA[<div class="qtext"></div>]]>'

I define several other elements during the process. At the end I save with:

tree._setroot(quiz)
tree.write("quiz.xml", encoding="UTF-8", xml_declaration=True)

The snippet in the exported XML file looks like:

<text>&lt;![CDATA[&lt;div class="qtext"&gt;</text>

Instead of:

<text><![CDATA[<div class="qtext"></text>

I have a working XML file as a standard example. My job is to copy it's structure. This is the reason why I have to include the HTML snippet. The file will be imported into Moodle.

1 Answers1

0

I have found a simple solution in a question regarded to CDATA writing in XML code.

https://stackoverflow.com/a/58392720/14865752

Basically it overrides the ET._escape_cdata(text) function. Eliminating all escape characters.