Saturday 18 October 2014

SAX Parser


SAX stands for Simple API for XML. SAX parsers operate on each piece of the XML document sequentially, whereas DOM parsers operates on the document as a whole.

SAX parser reads an XML document and fires an event whenever element started, element ends, document started, document ends etc., Because of the event-driven nature of SAX, processing documents is generally far faster than DOM-style parsers.



As shown in the figure, you can create SAXParser object using SAXParserFactory class. The parser wraps a SAXReader object. When the parser's parse() method is invoked, the reader invokes one of several callback methods implemented in the application. Those methods are defined by the interfaces ContentHandler, ErrorHandler, DTDHandler, and EntityResolver.

Let’s parse below xml file which contains employee data.


employee.xml
<?xml version="1.0"?>
<employees>
 <employee>
  <id>1</id>
  <firstName>Rama Krishna</firstName>
  <lastName>Gurram</lastName>
  <salary>100000</salary>
 </employee>
 <employee>
  <id>2</id>
  <firstName>Murali Krishna</firstName>
  <lastName>Bachu</lastName>
  <salary>110000</salary>
 </employee>
 <employee>
  <id>3</id>
  <firstName>Gopi</firstName>
  <lastName>Battu</lastName>
  <salary>55000</salary>
 </employee>
</employees>


Step 1: Create a handler class, by implementing any of the interfaces EntityResolver, DTDHandler, ContentHandler, ErrorHandler. For our convenience JAXP provides DefaultHandler class which provides default implementation for all the handler interfaces.
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ContentHandlerEx extends DefaultHandler{

    boolean empId = false;
    boolean empFirstName = false;
    boolean empLastName = false;
    boolean empSalary = false;

    @Override
    public void startDocument() throws SAXException {
        System.out.println("Processing of the document started");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("Processing of the document finished");
    }

    @Override
    public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException {
        //super.startElement(uri, localName, qName, attributes);
         System.out.println("Start Element : " + qName);

        if(qName.equalsIgnoreCase("id")){
            empId = true;
        }
        if(qName.equalsIgnoreCase("firstName")){
            empFirstName = true;
        }
        if(qName.equalsIgnoreCase("lastName")){
            empLastName = true;
        }
        if(qName.equalsIgnoreCase("salary")){
            empSalary = true;
        }
    }

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

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(empFirstName){
            System.out.println(new String(ch, start, length));
            empFirstName = false;
        }

        if(empLastName){
            System.out.println(new String(ch, start, length));
            empLastName = false;
        }

        if(empId){
            System.out.println(new String(ch, start, length));
            empId = false;
        }

        if(empSalary){
            System.out.println(new String(ch, start, length));
            empSalary = false;
        }
    }
}


Step 2: Instantiate parser object and pass the input file and the handler instance to the parse method of this parser object.
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class EmployeeParser {

    public static void main(String args[]) throws Exception{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        ContentHandlerEx handler = new ContentHandlerEx();
        saxParser.parse("D:\\employee.xml", handler);
    }
}


Output
Processing of the document started
Start Element : employees
Start Element : employee
Start Element : id
1
End Element : id
Start Element : firstName
Rama Krishna
End Element : firstName
Start Element : lastName
Gurram
End Element : lastName
Start Element : salary
100000
End Element : salary
End Element : employee
Start Element : employee
Start Element : id
2
End Element : id
Start Element : firstName
Murali Krishna
End Element : firstName
Start Element : lastName
Bachu
End Element : lastName
Start Element : salary
110000
End Element : salary
End Element : employee
Start Element : employee
Start Element : id
3
End Element : id
Start Element : firstName
Gopi
End Element : firstName
Start Element : lastName
Battu
End Element : lastName
Start Element : salary
55000
End Element : salary
End Element : employee
End Element : employees
Processing of the document finished










Prevoius                                                 Next                                                 Home

No comments:

Post a Comment