JAVA SAX DefaultHandler startCDATA() not firing

ⅰ亾dé卋堺 提交于 2019-12-10 11:23:27

问题


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!


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/6147682/java-sax-defaulthandler-startcdata-not-firing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!