We have launched a free Basic Kubernetes Course. Click here to enrol now! Kubernetes is a container orchestration platform used by enterprises worldwide and Kubernetes developers are in high demand!
SAX parsing is an efficient way to parse XML documents. Unlike DOM (Document Object Model) based parsers, SAX parsers do not load the XML document in memory. SAX parsers use callback methods to let the calling code know about XML related structure.
The key interfaces/classes are involved in SAX parsing are listed below:
- SAXParserFactory: This is the factory class used to configure and obtain SAX parser
- SAXParser: This represents the SAX parser and is responsible for parsing the XML document and report the XML structure using callback methods
- DefaultHandler: This class contains callback methods to handle SAX parser calls. It is a convenience base class as it provides default implementations for all of the callbacks.
Create employee.xml as shown below. This XML file shall be parsed by Aalto’s SAX parser.
Create TestSaxParsing as shown below.
Load Aalto’s SAX parser factory class (see line 18 below).
From the Factory, get an instance of
(see line 19 below).
Override the ‘startElement()’, ‘endElement()’ and ‘characters()’ callback method of
as shown in lines 28, 48 and 56 below). Aalto’s SAX parser shall call these methods while parsing the XML document.
Finally, pass the
class to the parser (see line 77 below).
The output of the program demonstrating SAX parsing is shown below: