import java.io.StringReader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.ext.DefaultHandler2; import com.ctc.wstx.sax.WstxSAXParser; public class WstxSAXParserTest { static class CDATASectionCounter extends DefaultHandler2 { private int cdataSectionCount; private int segmentCount; public void startCDATA() throws SAXException { cdataSectionCount++; } public void characters(char[] ch, int start, int length) throws SAXException { segmentCount++; } public int getCDATASectionCount() { return cdataSectionCount; } public int getSegmentCount() { return segmentCount; } } public static void main(String[] args) throws Exception { // Uncomment this to compare the result with the JRE SAX parser // SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); SAXParser parser = new WstxSAXParser(); StringBuffer buffer = new StringBuffer(""); CDATASectionCounter handler = new CDATASectionCounter(); parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler); parser.parse(new InputSource(new StringReader(buffer.toString())), handler); System.out.println("# of reported CDATA sections: " + handler.getCDATASectionCount()); System.out.println("# of segments: " + handler.getSegmentCount()); } }