How to read XML file in Java using SAX Parser ?

SAX parser is work differently with DOM parser, it does not load any XML document into memory and create some object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.

Let’s start to see how to read a XML file with SAX parser.

1. Create a XML file

Create a simple XML file as following

<?xml version="1.0"?>
  <lastname>mook kim</lastname>
  <lastname>yin fong</lastname>
  <nickname>fong fong</nickname>

2. Create a Java file

Use SAX parser to parse the XML file.

package com.mkyong.test;
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 ReadXMLFileSAX {
 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("c:\\file.xml", handler);
    } catch (Exception e) {

  • startDocument() and endDocument() – methods called at the start and end of an XML document.
  • startElement() and endElement() – methods called at the start and end of a document element.
  • characters() – method called with the text contents in between the start and end tags of an XML document element.

No comments:

Post a Comment

Please Provide your feedback here