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

Stage2

Problem Statement

Read Xml content from a given file and handle startDocument, endDocument, startElement and endElement. Read Attributes and Content.
Sample File

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <Question order="1" difficulty="2" time="5">
        This is Question1
        <option order="1" >
            This is option1 of Question1
        </option>
         <option order="2" >
            This is option2 of Question1
        </option>
         <option order="3" >
            This is option3 of Question1
        </option>
    </Question>
    <Question order="2" difficulty="3" time="15">
        This is Question2
        <option order="1" >
            This is option1 of Question2
        </option>
         <option order="2" >
            This is option2 of Question2
        </option>
         <option order="3" >
            This is option3 of Question2
        </option>
    </Question>
</test>


Solution


/*
 * ©Rajesh Patkar 
 */

package xmlfilereader;

import java.io.*;
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");
        System.out.println(Step1);
        System.out.println(Step2);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        System.out.println(Step3);
        SAXParser sp = spf.newSAXParser();
        System.out.println(Step4);
        String xmlFilePath = 
        "C:\\Users\\SONY\\Documents\\NetBeansProjects\\XmlFileReader\\"
        + "SampleXmlDocument.xml";
        System.out.println(Step5);
        System.out.println(Step6);
        FileInputStream fis = new FileInputStream(xmlFilePath);
        System.out.println(Step7);
        System.out.println(Step8);
        System.out.println(Step9);
        Handler h = new Handler();
        System.out.println(Step10);
        sp.parse(fis, h);
    } 
}

class Handler extends DefaultHandler
{

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

    @Override
    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);
        for(int i=0 ; i<attributes.getLength(); i++){
            System.out.println("Attribute Name = "+attributes.getQName(i));
            System.out.println("Attribute Value = "+attributes.getValue(i));
        }
    }

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

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

    @Override
    public void characters(char[] ch, int start, int length)throws SAXException{
        String s = new String(ch,start,length);
        System.out.println("Text Content:" + s);
    }
    
    
    
}




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 java.io for FileInputStream";
    public static String Step6 =
     "Creating instance of FileInputStream for reading the xml file";
    public static String Step7 =
     "Refering 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";            
    
}

Output

       run:
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 java.io 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
Text Content:
    
Start Element  Question
Attribute Name = order
Attribute Value = 1
Attribute Name = difficulty
Attribute Value = 2
Attribute Name = time
Attribute Value = 5
Text Content:
        This is Question1
        
Start Element  option
Attribute Name = order
Attribute Value = 1
Text Content:
            This is option1 of Question1
        
End Element option
Text Content:
         
Start Element  option
Attribute Name = order
Attribute Value = 2
Text Content:
            This is option2 of Question1
        
End Element option
Text Content:
         
Start Element  option
Attribute Name = order
Attribute Value = 3
Text Content:
            This is option3 of Question1
        
End Element option
Text Content:
    
End Element Question
Text Content:
    
Start Element  Question
Attribute Name = order
Attribute Value = 2
Attribute Name = difficulty
Attribute Value = 3
Attribute Name = time
Attribute Value = 15
Text Content:
        This is Question2
        
Start Element  option
Attribute Name = order
Attribute Value = 1
Text Content:
            This is option1 of Question2
        
End Element option
Text Content:
         
Start Element  option
Attribute Name = order
Attribute Value = 2
Text Content:
            This is option2 of Question2
        
End Element option
Text Content:
         
Start Element  option
Attribute Name = order
Attribute Value = 3
Text Content:
            This is option3 of Question2
        
End Element option
Text Content:
    
End Element Question
Text Content:

End Element test
End Document
BUILD SUCCESSFUL (total time: 0 seconds)