Jul 31, 2014

Java and XML tutorial with SAX parser - reading only

Q. What is a SAX Parser and when will you use it?
ASAX  (Simple API for XML) is a stream oriented API like StAX, but

  • Unlike StAX, which can read and write, but SAX can only read.
  • Unlike StAX, which can't validate schema, SAX can perform schema validation.
SAX works by iterating over the XML and call certain methods on a "handler" object , which listens to events like startDocument, startElement, characters, comments, processing instructions, endElement, and endDocument.

StAX your handler "pulls" the XML events out of the parser.

SAX  your parser pushes events into the handler

One major advantage of StAX over SAX is that the pull model allows subparsing of the XML input. You can extract out the element name, then the attributes, and then the characters (i.e. content)

Here is the sample code to read the employee XML.

package com.xml;


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;

public class SaxProcessing {

 public static void main(String[] args) {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  try {
   String xml = "<Employee><name type=\"first\">Peter</name><age>25</age></Employee>";

   SAXParser saxParser = factory.newSAXParser();
   DefaultHandler handler = new SaxHandler();
   saxParser.parse(new ByteArrayInputStream(xml.getBytes()), handler);

  } catch (Throwable err) {


Now the handler class that gets pushed with the events

package com.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxHandler extends DefaultHandler {
 public void startDocument() throws SAXException {
  System.out.println("Start document   : ");

 public void endDocument() throws SAXException {
  System.out.println("End document     : ");

 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  System.out.println("Start element    : " + qName);
  if (qName.equalsIgnoreCase("name")) {
   String attrName = attributes.getQName(0);
   String attrValue = attributes.getValue(0);
   System.out.println("Attribute        : " + attrName + "=" + attrValue);

 public void endElement(String uri, String localName, String qName) 
     throws SAXException {
  System.out.println("End element      : " + qName);

 public void characters(char ch[], int start, int length) throws SAXException {
  System.out.println("Start characters : " + new String(ch, start, length));


Start document   : 
Start element    : Employee
Start element    : name
Attribute        : type=first
Start characters : Peter
End element      : name
Start element    : age
Start characters : 25
End element      : age
End element      : Employee
End document     : 



Post a Comment

Subscribe to Post Comments [Atom]

<< Home