DefaultHandler in SAX Parser in Java
Last Updated :
25 Jul, 2022
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.
Similar Reads
Difference Between SAX Parser and DOM Parser in Java
There are two types of XML parsers namely Simple API for XML and Document Object Model. SAXDOMSAX (Simple API for XML), is the most widely adopted API for XML in Java and is considered the de-facto standard. Although it started as a library exclusive to Java, it is now a well-known API distributed o
4 min read
StAX XML Parser in Java
This article focuses on how one can parse a XML file in Java.XML : XML stands for eXtensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. Thatâs why, the design goals of XML emphasize simplicity, generality, and usability acros
6 min read
StAX vs SAX Parser in Java
Streaming the API for XML, called the StAX, is an API for reading and writing the XML Documents. It was introduced in Java 6 and is considered superior to SAX and DOM which are other methods in Java to access XML. Java provides several ways [APIs] to access XML. Traditionally, XML APIs are either- T
3 min read
Parse Context Class in Java
ParseContext class is a component of the Java packages org.apache.tika.parser, which is used to parse context and pass it on to the Tika (The Apache Tika toolkit detecting and extracts the metadata and the text from over a thousand different file types) parsers org.apache.tika.parser.ParseContext im
3 min read
Java.io.FilterReader class in Java
Abstract class for reading filtered character streams. The abstract class FilterReader itself provides default methods that pass all requests to the contained stream. Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields. Constructor : pr
3 min read
BodyContentHandler Class in Java
Apache Tika is a library that allows you to extract data from different documents(.PDF, .DOCX, etc.). In this tutorial, we will extract data by using BodyContentHandler.Next dependency that will be used is shown below: <dependency> <groupId>org.apache.tika < / groupId > <artifac
4 min read
Instant parse() method in Java with Examples
The parse() method of Instant class help to get an instance of Instant from a string value passed as parameter. This string is an instant in the UTC time zone. It is parsed using DateTimeFormatter.ISO_INSTANT. Syntax: public static Instant parse(CharSequence text) Parameters: This method accepts one
1 min read
String in Switch Case in Java
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated
3 min read
XML Output Factory in Java StAX
StAX provides various classes to create XML stream readers, writers, and events by using the XMLInputFactory, XMLOutputFactory, and XMLEventFactory classes. In this article we're going to study about XML Output Factory . The class javax.xml.stream.XMLOutputFactory is a root component of the Java StA
3 min read
ChoiceFormat parse() method in Java with Examples
The parse() method of java.text.ChoiceFormat class is used to get the limit value for particular format in ChoiceFormat object. Syntax: public Number parse(String text, ParsePosition status) Parameter: This method takes the following parameters: text: which is the text for which limit value have to
2 min read