Blog Archive

Java SAX parser example code - Part 2

A parser that implements SAX (i.e., a SAX Parser) functions as a stream parser, with an event-driven API. The user defines a number of callback methods that will be called when events occur during parsing. The SAX events include:

  • XML Text nodes
  • XML Element nodes
  • XML Processing Instructions
  • XML Comments

Events are fired when each of these XML features are encountered, and again when the end of them is encountered. XML attributes are provided as part of the data passed to element events.

SAX parsing is unidirectional; previously parsed data cannot be re-read without starting the parsing operation again.

Previous chapter - Java sax parser example code

SAX parser handler Java code

OrderXMLHandler class extends DefaultHandler to listen for call back events. We have to register this handler with the Sax parser to notify us of call back events.
package com.as400samplecode;

import java.util.ArrayList;

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

public class OrderXMLHandler extends DefaultHandler {

    boolean currentElement = false;
    String currentValue = null;
    Order order = null;
    OrderDetail orderDetail = null;
    ArrayList<OrderDetail> orderDetailList;
    ArrayList<Order> orderList = new ArrayList<Order>();

    public ArrayList<Order> getOrderList() {
        return orderList;

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

        currentElement = true;

        if (qName.equals("Order")){
            order = new Order();
            orderDetailList = new ArrayList<OrderDetail>();
        } else if (qName.equals("OrderLine")) {
            orderDetail = new OrderDetail();


    public void endElement(String uri, String localName, String qName)
    throws SAXException {

        currentElement = false;

        if (qName.equalsIgnoreCase("OrderID"))
        else if (qName.equalsIgnoreCase("CustNumber"))
        else if (qName.equalsIgnoreCase("OrderDate"))
        else if (qName.equalsIgnoreCase("CustPONumber"))
        else if (qName.equalsIgnoreCase("OrderTotal"))
        else if (qName.equalsIgnoreCase("OrderlineID"))
        else if (qName.equalsIgnoreCase("ItemID"))
        else if (qName.equalsIgnoreCase("ItemDescription"))
        else if (qName.equalsIgnoreCase("Quantity"))
        else if (qName.equalsIgnoreCase("Price"))
        else if (qName.equalsIgnoreCase("Order")){
        else if (qName.equalsIgnoreCase("OrderLine"))

    public void characters(char[] ch, int start, int length)
    throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;



SAX parser Java main method implementing the parser

package com.as400samplecode;

import java.util.ArrayList;

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

public class ParseOrder {

    public static void main( String[] args )

        try {
            OrderXMLHandler myXMLHandler = new OrderXMLHandler();
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse(new File("Order.xml"),myXMLHandler);

            ArrayList<Order> orderList = myXMLHandler.getOrderList();
            for(int i=0;i<orderList.size();i++){
                Order order = orderList.get(i);
                System.out.println("OrderID: " + order.getOrderID() + " ----------->");
                System.out.println("Type: " + order.getOrderType());
                System.out.println("Date: " + order.getOrderDate());
                System.out.println("Customer: " + order.getOrderType());
                System.out.println("PO: " + order.getCustPONumber());
                System.out.println("Total: " + order.getOrderTotal());
                ArrayList<OrderDetail> orderDetailList = order.getOrderDetail();
                for(int j=0;j<orderDetailList.size();j++){
                    OrderDetail orderDetail = orderDetailList.get(j);
                    System.out.println("LineID: " + orderDetail.getOrderlineID() + " ----->");
                    System.out.println("Item: " + orderDetail.getItemID());
                    System.out.println("Description: " + orderDetail.getItemDescription());
                    System.out.println("Price: " + orderDetail.getPrice());
                    System.out.println("Quantity: " + orderDetail.getQuantity());

        catch (Exception e) {
        finally {