How to create a CDATA XML node

Peter Hansen peter at engcorp.com
Thu Mar 20 16:52:40 EST 2003


Antun Karlovac wrote:
> 
> How do I implement a CDATA node in Python? I'm using dom.minidom as
> follows:-
> 
>         impl = xml.dom.minidom.getDOMImplementation()
>         doc = impl.createDocument( None, "element", None )
>         root = doc.documentElement
> 
> Then I want to create my CDATA node. The reason I want a CDATA node is
> because I want to preserve all the HTML formatting tags (i.e. the
> <p>'s here) in the text node:-
> 
>         longDescText = '<p>There will be more text here.</p>'
>         longDescText = longDescText
>         longDescNode = doc.createElement( 'longdescription' )
>         longDescTextNode = doc.createTextNode( longDescText )
>         longDescNode.appendChild( longDescTextNode )
>         root.appendChild( longDescNode )
> 
> I can't anything in either the docs, or the Python & XML O'Reilly
> book. Am I blind or summat?

CDATA is, I believe, used only in the XML source file itself 
to in effect "escape" the content and avoid portions thereof
from being interpreted as XML syntax.

When using DOM, you should be able just to create a simple 
text node.  Upon export, technically, the toxml() method or
whatever means you are using must scan the content of the
text node and perform appropriate escaping to ensure that the
output text of that node will not be mistaken for XML syntax.
This might use lots of &#x1234; crap, or it might be smart
enough to see that using CDATA would be better, but I don't
believe there's any defined, standard way of forcing a CDATA
to be used on output.  I bet some implementations don't even
scan their text node data before dumping it either...

-Peter




More information about the Python-list mailing list