Friday, September 27, 2013

Creating CDATA node using Python minidom

Code sample for creating xml with CDATA node using python (xml.dom.minidom)

Sample Code

from xml.dom import minidom
doc = minidom.Document()
root = doc.createElement("Test")
root.setAttribute("Name","Soorej")
cdata = doc.createCDATASection("Some test value")
cdv = doc.createElement("cdatanode")
cdv.setAttribute("Sev","1")
cdv.appendChild(cdata)
root.appendChild(cdv)
doc.appendChild(root)
print doc.toxml()

Output:

<?xml version="1.0" ?>
<Test Name="Soorej"><cdatanode Sev="1"><![CDATA[Some test value]]></cdatanode></Test>

No comments: