Skip to content

Instantly share code, notes, and snippets.

@zlalanne
Created June 5, 2013 05:48
Show Gist options
  • Save zlalanne/5711847 to your computer and use it in GitHub Desktop.
Save zlalanne/5711847 to your computer and use it in GitHub Desktop.
Adds CDATA support to Python ElementTree
import xml.etree.ElementTree as ET
def CDATA(text=None):
element = ET.Element('![CDATA[')
element.text = text
return element
ET._original_serialize_xml = ET._serialize_xml
def _serialize_xml(write, elem, encoding, qnames, namespaces):
if elem.tag == '![CDATA[':
write("<%s%s]]>%s" % (elem.tag, elem.text, elem.tail))
return
return ET._original_serialize_xml(
write, elem, encoding, qnames, namespaces)
ET._serialize_xml = ET._serialize['xml'] = _serialize_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment