By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,587 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,587 IT Pros & Developers. It's quick & easy.

saxutils.XMLGenerator and CDATA

P: n/a
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

Jan 20 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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>&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
ro*********@gmail.com

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

Jan 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.