saxutils.XMLGenerator and CDATA

C

chris

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>&lt;![CDATA[x&amp;&lt;&gt;xxx]]&gt;</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?

Thanks,
Chris
 
R

Robert Kern

chris said:
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>&lt;![CDATA[x&amp;&lt;&gt;xxx]]&gt;</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
(e-mail address removed)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top