python CDATA in subelement

1.8k Views Asked by At

I'm trying to add CDATA to an subElement of XML. But the XML keeps converting the < character to &lt;.

I've read some posts here that do something with CDATA, but I can't believe it's this hard to make it work. Besides that, I can't get those examples working.

Below a part of my code simplified, using python 3.4.

import xml.etree.cElementTree as ET from xml.dom import minidom

class MyClass():
    def __init__(self):
        self.xml = None

    def generateXML(self):
        self.xml = ET.Element("AVXML")
        row = ET.SubElement(self.xml, "ROW")
        ET.SubElement(row, "DATA").text = "<![CDATA[ <ART_HDR.COMMENT1>.PDF ]]>"

    def saveXML(self):
        rough_string = ET.tostring(self.xml, 'Windows-1252')
        reparsed = minidom.parseString(rough_string)
        prettyxml = reparsed.toprettyxml(indent="  ", encoding="Windows-1252")

        print(prettyxml)

        f = open("filetosave.xml", "wb")
        f.write(prettyxml)
        f.close()

m = MyClass()
m.generateXML()
m.saveXML()

This however generates:

<DATA>&lt;![CDATA[ &lt;ART_HDR.COMMENT1&gt;.PDF ]]&gt;</DATA>

Instead of

<DATA><![CDATA[ <ART_HDR.COMMENT1>.PDF ]]></DATA>
1

There are 1 best solutions below

0
On

Oke, I used this comment and got it working.

import xml.etree.cElementTree as ET from xml.dom import minidom

class MyClass():
    def __init__(self):
        self.xml = None

    def generateXML(self):
        self.xml = ET.Element("AVXML")
        row = ET.SubElement(self.xml, "ROW")
        data = " <ART_HDR.COMMENT1>.PDF "
        cdata = ET.SubElement(row, "DATA")
        cdata.append(ET.Comment(' --><![CDATA[' + data.replace(']]>', ']]]]><![CDATA[>') + ']]><!-- '))

    def saveXML(self):
        rough_string = ET.tostring(self.xml, 'Windows-1252')
        reparsed = minidom.parseString(rough_string)
        prettyxml = reparsed.toprettyxml(indent="  ", encoding="Windows-1252")

        f = open("filetosave.xml", "wb")
        f.write(prettyxml)
        f.close()

m = MyClass()
m.generateXML()
m.saveXML()