How to read XML file using SAX Parser

In this tutorial we are going to see how to read XML file using SAX parser.

ReadXMLFile.java

package com.ehowtonow.xml.sax;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
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 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  try {
   SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
   SAXParser parser = saxParserFactory.newSAXParser();
   DefaultHandler defaultHandler = new DefaultHandler() {
    boolean firstName = false;
    boolean lastName = false;

    public void startElement(String uri, String localName,
      String qName, Attributes attribute) throws SAXException {
     System.out.println("Start Element : " + qName);
     if (qName.equalsIgnoreCase("FIRSTNAME")) {
      firstName = true;
     }
     if (qName.equalsIgnoreCase("LASTNAME")) {
      lastName = 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 (firstName) {
      System.out.println("First Name : "
        + new String(ch, start, length));
      firstName = false;
     }
     if (lastName) {
      System.out.println("Last Name : "
        + new String(ch, start, length));
      lastName = false;
     }
    }
   };
   parser.parse(new File("C:\\jtc\\student.xml"), defaultHandler);
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

student.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<college>
<student>
<firstname>Sathish</firstname>
<lastname>J</lastname>
</student>
</college>

Now you can run the program see the following output in console.

Start Element : college
Start Element : student
Start Element : firstname
First Name : Sathish
End Element : firstname
Start Element : lastname
Last Name : J
End Element : lastname
End Element : student
End Element : college


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. XML SAX Parser Introduction
  2. How to read XML file using SAX Parser
  3. Read XML File as List of Object using SAX Parser
  4. How Read UTF-8 character XML file using SAX Parser

Be the first to comment

Leave a Reply