Open In App

DefaultHandler in SAX Parser in Java

Last Updated : 25 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

SAX is nothing but a Simple API for XML and it is an event-based parser for XML documents. It differs from a DOM parser in such a way that a parse tree is not created by a SAX parser. The process is done in a sequential order starting at the top of the document and ending with the closing of the ROOT element. Timely notifications are also being sent while parsing is happening. Tokens also followed in the same order. An event handler must be registered with the parser. SAX parser is best whenever there is a linear fashion of looking at the document and that too if it is not deeply nested, it is well and good. Mostly for large XML documents, SAX is better than DOM, it occupies huge memory while processing. It is a top-to-bottom sequential approach and hence no random access is allowed also if we need to carry out the operations part by part, the code needs to be written on its own and processed.

The org.xml.sax.helpers.DefaultHandler class is the base class for “listeners” in SAX 2.0. Any user handler class should extend the DefaultHandler and override the methods namely startElement, endElement, characters, etc. Let us see the functionalities of each and every method of DefaultHandler

Methods

Description

void startDocument()  This is called at the beginning of a document.
void endDocument()  This is called at the end of a document
void startElement(String uri, String localName, String qName, Attributes atts)  This is called at the beginning of an element. With different input parameters, attributes are correctly mapped
void endElement(String uri, String localName,String qName)  This is called at the end of an element. With different input parameters, attributes are correctly mapped
void characters(char[] ch, int start, int length)  This is called when character data is encountered.
void ignorableWhitespace( char[] ch, int start, int length)  This is called when a DTD is present and whitespace which cannot be ignored is encountered.
void processingInstruction(String target, String data)  This is called when a processing instruction is recognized.
void setDocumentLocator(Locator locator))  This Provides a Locator and it can be used to identify the exact positions in the document.
void skippedEntity(String name)  This is called when an unresolved entity is present.
void startPrefixMapping(String prefix, String uri)  This is called when a new namespace mapping is defined.
void endPrefixMapping(String prefix)  This is called when a namespace definition ends its scope.

Example

Let us see a sample program that uses the SAX Parser method using  DefaultHandler. We are creating a class called “EmployeeHandler” that extends “DefaultHandler” and hence we need to override the methods namely startElement, endElement, characters, etc.

Our input file: employee.txt

XML




<?xml version = "1.0"?>
<class>
   <employee id = "300">
      <firstname>Geek</firstname>
      <lastname>Person1</lastname>
      <nickname>Rachel</nickname>
      <salary>85000</salary>
   </employee>
     
   <employee id = "400">
      <firstname>Geek</firstname>
      <lastname>Person2</lastname>
      <nickname>Monica</nickname>
      <salary>75000</salary>
   </employee>
     
   <employee id = "500">
      <firstname>Geek</firstname>
      <lastname>Person3</lastname>
      <nickname>Phoebe</nickname>
      <salary>70000</salary>
   </employee>
</class>


EmployeeSAXParserDemo.java

Java




import java.io.File;
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 EmployeeSAXParserDemo {
    public static void main(String[] args) {
      try {
         File inputFile = new File("employee.txt");
         SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
         SAXParser saxParser = saxParserFactory.newSAXParser();
         EmployeeHandler employeeHandler = new EmployeeHandler();
         saxParser.parse(inputFile, employeeHandler);     
      } catch (Exception e) {
         e.printStackTrace();
      }
   }     
}
  
class EmployeeHandler extends DefaultHandler {
   boolean isFirstName = false;
   boolean isLastName = false;
   boolean isNickName = false;
   boolean isSalary = false;
  
   @Override
   public void startElement(
      String uri, String localName, String attributeName, Attributes attributes)
      throws SAXException {
        
      if (attributeName.equalsIgnoreCase("employee")) {
         String id = attributes.getValue("id");
         System.out.println("Employee ID : " + id);
      } else if (attributeName.equalsIgnoreCase("firstname")) {
         isFirstName = true;
      } else if (attributeName.equalsIgnoreCase("lastname")) {
         isLastName = true;
      } else if (attributeName.equalsIgnoreCase("nickname")) {
         isNickName = true;
      }
      else if (attributeName.equalsIgnoreCase("salary")) {
         isSalary = true;
      }
   }
  
   @Override
   public void endElement(String uri, 
      String localName, String attributeName) throws SAXException {
      if (attributeName.equalsIgnoreCase("employee")) {
         System.out.println("End Element :" + attributeName);
      }
   }
  
   @Override
   public void characters(char character[], int start, int length) throws SAXException {
      if (isFirstName) {
         System.out.println("First Name: " + new String(character, start, length));
         isFirstName = false;
      } else if (isLastName) {
         System.out.println("Last Name: " + new String(character, start, length));
         isLastName = false;
      } else if (isNickName) {
         System.out.println("Nick Name: " + new String(character, start, length));
         isNickName = false;
      } else if (isSalary) {
         System.out.println("Salary: " + new String(character, start, length));
         isSalary = false;
      }
   }
}


Output:

On running the above code, the output is as follows

 

Note:

For handling exceptions, 3 methods need to be overridden in the EmployeeHandler if required

public void warning(SAXParseException e) throws SAXException {
}
public void error(SAXParseException e) throws SAXException {
}
public void fatalError(SAXParseException e) throws SAXException {
}

Conclusion

By overriding the methods present in DefaultHandler, any handler java file will process using SAX parser and get the perfect results.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads