In this blog, I will show you how to read an XML file via Java SAX parser. SAX Parsers are faster than DOM parser because it doesn’t load complete XML into memory and read xml document sequentially. SAXParser are little bit confusing in start but if you are working on large XML document, it provides more efficient way to read XML than DOM Parser.

Sample xml file

/home/anand/staff.xml

<?xml version="1.0"?>
<company>
	<staff id="121">
		<firstname>Anand</firstname>
		<lastname>Kushwaha</lastname>
		<nickname>anand</nickname>
		<salary>1000</salary>
	</staff>
	<staff id="122">
		<firstname>Aman</firstname>
		<lastname>Goel</lastname>
		<nickname>aman</nickname>
		<salary>2000</salary>
	</staff>
</company>

SAX XML Parser Example

This example shows you how to get the node by “name”, and display the value.

package com.jft.seo;

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 ReadXMLFile {

    public static void main(String argv[]) {

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                boolean bfname = false;
                boolean blname = false;
                boolean bnname = false;
                boolean bsalary = false;

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

                    System.out.println("Start Element :" + qName);

                    if (qName.equalsIgnoreCase("FIRSTNAME")) {
                        bfname = true;
                    }

                    if (qName.equalsIgnoreCase("LASTNAME")) {
                        blname = true;
                    }

                    if (qName.equalsIgnoreCase("NICKNAME")) {
                        bnname = true;
                    }

                    if (qName.equalsIgnoreCase("SALARY")) {
                        bsalary = true;
                    }

                }

                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 {

                    if (bfname) {
                        System.out.println("First Name : " + new String(ch, start, length));
                        bfname = false;
                    }

                    if (blname) {
                        System.out.println("Last Name : " + new String(ch, start, length));
                        blname = false;
                    }

                    if (bnname) {
                        System.out.println("Nick Name : " + new String(ch, start, length));
                        bnname = false;
                    }

                    if (bsalary) {
                        System.out.println("Salary : " + new String(ch, start, length));
                        bsalary = false;
                    }
                }
            };
            saxParser.parse("/home/anand/staff.xml", handler);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output :

Start Element :company
Start Element :staff
Start Element :firstname
First Name : Anand
End Element :firstname
Start Element :lastname
Last Name : Kushwaha
End Element :lastname
Start Element :nickname
Nick Name : anand
End Element :nickname
Start Element :salary
Salary : 1000
End Element :salary
End Element :staff
Start Element :staff
Start Element :firstname
First Name : Aman
End Element :firstname
Start Element :lastname
Last Name : Goel
End Element :lastname
Start Element :nickname
Nick Name : aman
End Element :nickname
Start Element :salary
Salary : 2000
End Element :salary
End Element :staff
End Element :company