5

This is the Python equivalent of the Java question How to output a CDATA section from a Sax XmlHandler

Neither xml.sax.saxutils.XMLGenerator or lxml.sax.ElementTreeContentHandler says anything about CDATA sections. How can I get it to output CDATA?

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970

2 Answers2

9

You could do it straight in your code using

from xml.sax.saxutils import XMLGenerator


xml = XMLGenerator()
xml.startDocument()
xml.startElement('item', {})
content = '<p>Stuff</p>'
cdata = '<![CDATA[{}]]>'.format(content)
xml.ignorableWhitespace(cdata)
xml.endElement('item')
xml.endDocument()

Or extend the XMLGenerator class with a new function

from xml.sax.saxutils import XMLGenerator


class _XMLGenerator(XMLGenerator):
    def cdata(self, content):
        cdata = '<![CDATA[{}]]>'.format(content)
        self.ignorableWhitespace(cdata)

xml = _XMLGenerator()
xml.startDocument()
xml.startElement('item', {})
content = '<p>Stuff</p>'
xml.cdata(content)
xml.endElement('item')
xml.endDocument()

The reason I don't use xml.characters(content) is because it calls the xml.sax.saxutils.escape function, which in turns escapes &, < and >.

niklasae
  • 1,097
  • 1
  • 9
  • 8
2

You can use xml.dom.minidom like this:

doc = xml.dom.minidom.Document()
article = doc.createElement('article')
content = doc.createCDATASection('<p>Any CDATA</p>')
article.appendChild(content)
mzjn
  • 48,958
  • 13
  • 128
  • 248
Jackeriss
  • 31
  • 3
  • Yes, the `Document.createCDATASection()` method works, but it is missing from the documentation: https://docs.python.org/3/library/xml.dom.html#document-objects. – mzjn Dec 23 '21 at 15:34