2

I am trying to parse and detect the start of the CDATA within a tag like:

<child><![CDATA[data goes here]]></child>

I have a class that extends the Default handler

class MyXmlDoc extends DefaultHandler{

with methods for startElement() and endElement() that fire correctly but the startCDATA() never fires. My characters() method picks up the 'data goes here' so it appears that the CDATA 'wrapper' is detected but ???

Thanks for any insight!

jjdebarros
  • 115
  • 1
  • 2
  • 6

2 Answers2

4

CDATA is a lexical event. Regular handlers (content handler, error handler) do not process these events. You need to set a lexical handler for your reader, if it supports having one. Lexical handler is a SAX2 extension so Java XMLReader uses setProperty method for setting it.

See: http://download.oracle.com/javase/6/docs/api/org/xml/sax/XMLReader.html#setProperty%28java.lang.String,%20java.lang.Object%29 and http://download.oracle.com/javase/6/docs/api/org/xml/sax/ext/LexicalHandler.html

jasso
  • 13,736
  • 2
  • 36
  • 50
1

Is your data getting escaped in there? how are you writing the xml doc?

This may help you: How to output a CDATA section from a Sax XmlHandler

This is pretty verbose too: http://www.coderanch.com/t/127987/XML/read-cdata-sax-parser

Community
  • 1
  • 1
Durandal
  • 5,575
  • 5
  • 35
  • 49