Sunday, August 5, 2012

SAX 2048 byte issue on Java XML parsing

SAX parser code has an issue for files or messages of greater than 2048 bytes. Parser only reads in 2048 bytes at a time which could lead to an error if the 2048-byte split the data element.

The solution to this is to introduce a temporary variable on the callback method. Here is code snippet.


public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

public void startElement (String uri, String localName, String qName, Attributes attr) throws SAXException{
     value = new StringBuffer();     if(qName.equalsIgnoreCase("clientDets")) {          ncstClient = new NcstClient();          ncstClient.setType(attributes.getValue("type"));     }}

public void characters(char[] ch, int start, int length) throws SAXException {     value.append(new String(ch, start, length));}