SAX Parser – Reading CDATA
CDATA is the section in XML Document which should not be parsed by XML parser implementation. This section can be used to pass the data which generally does not comply with XML syntax like a javascript function. If you need to transfer a javascript function in your XML document or a JSON formatted data which may have some characters, which are not allowed in XML syntax, you can use this section.
This section is generally retrieved as a Node but XML parsers ignore its content so after getting the node you can extract the content of this node. Parsing is generally easier in DOM approach and you can use its API to get the CDATA node loaded into the memory. DOM approach is little bit expensive in case you are trying to parse a big XML document.
While doing the SAX parsing you generally get events when parser encounters a node and your can get callback on an handler, Use following code to retrieve CDATA for SAX parsing. Below I tried to get a JSON string from CDATA section:
import java.io.File; import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import net.sf.json.JSONObject; import org.xml.sax.SAXException; import org.xml.sax.ext.DefaultHandler2; import org.xml.sax.helpers.DefaultHandler; public class CDATAReader { private SAXParser parser; private DefaultHandler handler; public void parseXmlFile(File file) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); parser = factory.newSAXParser(); handler = new CDATAParser(); parser.parse(file, handler); } catch (SAXException e) { // A parsing error occurred; the xml input is not valid } catch (IOException e) { // Unable to read file } } private class CDATAParser extends DefaultHandler2 { public synchronized void characters(char[] cbuf, int start, int len) { JSONObject ob = JSONObject.fromString(new String(cbuf, start, len)); System.out.println(ob.toString();); } } public static void main(String[] args) { try { File file = new File(args[0]); CDATAReader reader = new CDATAReader(); reader.parseXmlFile(file); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }
Most Commented Posts
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Have you looked at vtd-xml?