saxutils.XMLGenerator and CDATA

Robert Kern robert.kern at gmail.com
Fri Jan 20 18:45:21 EST 2006


chris wrote:
> Confused somewhat xml newbie.  I'm trying to output xml with a CDATA
> section, using saxutils.XMLGenerator, but the output contains escaped
> '<' and '>'  and '&' from the CDATA element.  Shouldn't it be spit out
> intact?
> 
> Here's code and output:
> 
> from xml.sax import saxutils
> import sys
> handler = saxutils.XMLGenerator(sys.stdout)
> handler.startDocument()
> handler.startElement('sometag', {})
> handler.characters('<![CDATA[x&<>xxx]]>')
> handler.endElement('sometag')
> handler.endDocument()
> 
> Produces:
> <?xml version="1.0" encoding="iso-8859-1"?>
> <sometag><![CDATA[x&<>xxx]]></sometag>
> 
> I was expecting to see
> <?xml version="1.0" encoding="iso-8859-1"?>
> <sometag><![CDATA[x&<>xxx]]></sometag>
> 
> What am I doing wrong here?

The <![CDATA[]]> crud is markup, not character data. You are passing it in as
characters (hence the method's name "characters"), so saxutils.XMLGenerator is
treating it as such.

<!CDATA[]]> sections are intended for human authoring, not computer authoring.
If you want to keep your sanity, just don't do it.

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list