ScottShea has asked for the wisdom of the Perl Monks concerning the following question:

Hey there, I have been trying to Parse the CDATA section of an XML string and have come up short. Does anyone know how to set XML::Parser to grab the CDATA section? Scott

Replies are listed 'Best First'.
Re: CDATA Parsing for XML::Parser
by mirod (Canon) on Sep 18, 2003 at 07:52 UTC

    The start of the CDATA section triggers the CdataStart handler, the end the CdataEnd handler, and the content is caught by the Char handler. So all you need is to set a flag when the CDATA section starts, then capture characters while the flag is set, then unset the flag at the end of the section.

    Now for some unwanted advice:

    • CDATA sections are just syntactic sugar, a shortcut to avoid escaping too many >'s and &'s. They are never garanteed to be there, you just cannot enforce it. So relying on a CDATA section being present is a little dangerous.
    • If you are just starting with a project, you probably shouldn't be using XML::Parser. It is an old module, and its API is not standard at all. If you can install it, XML::LibXML is most likely a better choice: it is faster and way, WAY more powerful. Or of course XML::Twig ;--)

Re: CDATA Parsing for XML::Parser
by rupesh (Hermit) on Sep 18, 2003 at 07:21 UTC
    Hey,
    Im really not sure about XML Parsers... but while surfing thru the net, I just got this
    The next question also gave a hint of how to "grab" the character data.

    Hope they were of some use!

    Did you ever notice that when you blow in a dog's face, it gets mad at you but when you take him on a car ride,he sticks his head out the window and likes it?