How to Parse XML file using SAX Parser?

In my previous article I had written the example program for parsing a simple XML file using the DOM parser. As I have mentioned in the earlier posts, DOM and SAX are the two popular parser used for reading and manipulating the XML files. If you use any of the XML utility like JAXB for Java / XML binding, they are internally using any of these parser. DOM and SAX are the core APIs for reading the XML files. Both are working in the different way.

  1. DOM loads the entire XML file into meorty and then retrives the XML elements. It is good for the small XML files. If the XML file is huge in size, it will impact the performance and consumes lot of memory.
  2. SAX parser uses the event driven model to find an element. It traverses the entire XML file to find the elements.  SAX parser is faster and less memory then a DOM parser.

SAX uses the callback functions the class org.xml.sax.helpers.DefaultHandler for reading the XML elements. The following are the callback functions:

  1. startDocument and endDocument: This mthods are are called at the start and end of the document.
  2. startElement and endElement: This methods are called at the time when parser reading the start tag and end tag for each elements.
  3. characters: This method is called when it finds text content inside the start and end tag of an element.

Look at the example. Before that look how the folder structure for this example is setup. Keep the XML at the root of the project directory.

XML SAX Parser

1. Create A Simple XML File


<?xml version="1.0"?>
	<employee id="001">
	<employee id="002">

2. SAX Parser For Parsing The XML File


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

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

public class JavaSAXParser extends DefaultHandler {
	public static void main(String args[]) {
		JavaSAXParser xmlParser = new JavaSAXParser();

	public void parseXmlUsingSAX() {
		try {
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			SAXParser saxParser = saxParserFactory.newSAXParser();
			DefaultHandler defaultHandler = new DefaultHandler() {
				String firstNameTag = "close";
				String lastNameTag = "close";

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

					if (qName.equalsIgnoreCase("FirstName")) {
						firstNameTag = "open";
					if (qName.equalsIgnoreCase("lastName")) {
						lastNameTag = "open";

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

					if (firstNameTag.equals("open")) {
						System.out.println("First Name : "
								+ new String(ch, start, length));
					if (lastNameTag.equals("open")) {
						System.out.println("Last Name : "
								+ new String(ch, start, length));

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

					if (qName.equalsIgnoreCase("firstName")) {
						firstNameTag = "close";
					if (qName.equalsIgnoreCase("lastName")) {
						lastNameTag = "close";
			saxParser.parse("employees.xml", defaultHandler);
		} catch (Exception e) {

The output for the above program will be:

SAX Parser Output

I hope the above example helped you to understand the SAX implementation. If you have any questions, please write it in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!