Javahub‎ > ‎CodeBook‎ > ‎Xml‎ > ‎1) Reading Xml file using SAX API‎ > ‎


Problem Statement

Read Xml content from a given file and handle startDocument, endDocument, startElement and endElement events. Display attributes and Contents.
Sample File

       <?xml version="1.0" encoding="UTF-8"?>
                 This is Question1


 * ©Rajesh Patkar 

package xmlfilereader;

import javax.xml.parsers.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.*;
import static xmlfilereader.Prompter.*;

public class XmlFileReader {

    public static void main(String[] args) throws Exception {
        System.out.println("Reading Xml File using SAX API");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        String xmlFilePath = 
        + "SampleXmlDocument.xml";
        FileInputStream fis = new FileInputStream(xmlFilePath);
        Handler h = new Handler();
        sp.parse(fis, h);

class Handler extends DefaultHandler

    public void endElement(String uri, String localName, String qName) 
            throws SAXException {
        super.endElement(uri, localName, qName);
        System.out.println("End Element "+ qName);

    public void startElement(String uri, String localName, 
            String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        System.out.println("Start Element  "+ qName);

    public void endDocument() throws SAXException {
        System.out.println("End Document");

    public void startDocument() throws SAXException {
        System.out.println("Start Document");

class Prompter
    public static String Step1 =
     "referring imported package javax.xml.parsers for SAXParserFactory ";
    public static String Step2 =
     "Creating instance of SAXParserFactory";
    public static String Step3 =
     "Creating instance of SAXParser";
    public static String Step4 =
     "Creating String that stores path to xml file";
    public static String Step5 =
     "Refering imported package for FileInputStream";
    public static String Step6 =
     "Creating instance of FileInputStream for reading the xml file";
    public static String Step7 =
     "Referring package org.xml.sax.handler for DefaultHandler";
    public static String Step8 =
     "Creating 'Handler' a subclass of DefaultHandler for"
            + " handling Parsing events";
    public static String Step9 =
     "Handler overrides startDocument, endDocument, startElement, endElement";
    public static String Step10 = 
     "Invoke parse method on parser object"
            + "\npass FileInputStream and Handler instance as parameters";


Reading Xml File using SAX API
refering imported package javax.xml.parsers for SAXParserFactory 
Creating instance of SAXParserFactory
Creating instance of SAXParser
Creating String that stores path to xml file
Refering imported package for FileInputStream
Creating instance of FileInputStream for reading the xml file
Refering package org.xml.sax.handler for DefaultHandler
Creating 'Handler' a subclass of DefaultHandler for handling Parsing events
Handler overrides startDocument, endDocument, startElement, endElement
Invoke parse method on parser object
pass FileInputStream and Handler instance as parameters
Start Document
Start Element  test
Start Element  question
End Element question
End Element test
End Document
BUILD SUCCESSFUL (total time: 0 seconds)