saxutils.XMLGenerator and CDATA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris

    #1

    saxutils.XMLGenerator and CDATA

    Confused somewhat xml newbie. I'm trying to output xml with a CDATA
    section, using saxutils.XMLGen erator, 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.XMLGen erator(sys.stdo ut)
    handler.startDo cument()
    handler.startEl ement('sometag' , {})
    handler.charact ers('<![CDATA[x&<>xxx]]>')
    handler.endElem ent('sometag')
    handler.endDocu ment()

    Produces:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <sometag>&lt; ![CDATA[x&amp;&lt;&gt;x xx]]&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

  • Robert Kern

    #2
    Re: saxutils.XMLGen erator and CDATA

    chris wrote:[color=blue]
    > Confused somewhat xml newbie. I'm trying to output xml with a CDATA
    > section, using saxutils.XMLGen erator, 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.XMLGen erator(sys.stdo ut)
    > handler.startDo cument()
    > handler.startEl ement('sometag' , {})
    > handler.charact ers('<![CDATA[x&<>xxx]]>')
    > handler.endElem ent('sometag')
    > handler.endDocu ment()
    >
    > Produces:
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <sometag>&lt; ![CDATA[x&amp;&lt;&gt;x xx]]&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?[/color]

    The <![CDATA[]]> crud is markup, not character data. You are passing it in as
    characters (hence the method's name "characters "), so saxutils.XMLGen erator 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@gma il.com

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

    Comment

    Working...