Buse Bozkurt
4 min readAug 31, 2020

WHAT IS JAVA SAX PARSER? HOW TO CREATE A HANDLER CLASS?

Hello, in this article, we will see what SAX Parser is, when it should be used and how to parse the XML file using SAX Parser.

XML parse aims to reach data by parsing the XML file. Java offers various ways to parse XML files. Before explaining how to parse XML files with SAX Parser, let’s take a look at another widely used library, DOM Parser, and see the difference between the two.

DOM Parser

DOM; It is short for Document Object Model. DOM Parser method places the XML file from the root element to the end of the file in a computer science tree in memory. Since the entire file is in memory, the DOM can navigate between nodes, create and remove nodes.

SAX Parser

SAX; It is short for Simple Api For Xml. Unlike DOM Parser, the SAX Parser method reads the file as a stream and parse it. The parsing process is carried out with 3 basic callback methods that we will see in the rest of the article.

When Should We Use SAX Parser?

  • When you want to access the data in the XML file linearly from top to bottom
  • When we want to parse very large XML files
  • SAX Parser is used when random access to the XML file is not required because it does not store the file in memory.

We have seen in which situations SAX Parser is used and the difference between it and DOM Parser, now let’s look at how parsing is done with SAX Parser.

SAX Parser Example

Below is a part of the XML file that we will parse as an example.

<xn:SubNetwork id=”SIAR5">
<xn:MeContext id=”SI845">
<xn:VsDataContainer id=”SI845">
<xn:attributes>
<xn:vsDataType>vsDataMeContext</xn:vsDataType>
<es:vsDataMeContext>
<es:platformType>ASS</es:platformType>
<es:MeContextId>SI845</es:MeContextId>
<es:neType>CRP</es:neType>
</es:vsDataMeContext>
</xn:attributes>
</xn:VsDataContainer>
</xn:MeContext>
</xn:SubNetwork>

In order to parse the XML file using SAX Parser, we must first create our own Handler class. In order to create the Handler class, we have to extend it from the org.xml.sax.helpers.DefaultHandler class. The DefaultHandler class contains callback methods. These are StartElement, EndElement, CharacterData, StartDocument, EndDocument methods.

Below is an example of the My Handler class created by extending the DefaultHandler class.

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.*;

public class My_Handler extends DefaultHandler {
public void startElement(String nameSpaceURI, String localName, String qName, Attributes atts) {
....
}
public void characters(char ch[], int start, int length) throws SAXException {
....
}
public void endElement(String nameSpaceURI, String localName, String qName){
....
}
}

Let’s look at what the callback methods in the My_Handler class we created are and how they should be used.

Start Element

SAX Parser starts parsing the XML file and when it sees the start tag in the file, the startElement() method is called. Information about the tag is sent as a parameter to this method. The startElement() method example is as follows.

public void startElement(String nameSpaceURI, String localName, String qName, Attributes atts) {

if (localName.equalsIgnoreCase("SubNetwork")){
...
}

else if (specialMos.contains(localName.toUpperCase())){
...
}

else if (localName.equalsIgnoreCase("VsDataContainer")){
...
}

else if (localName.equalsIgnoreCase("vsDataFormatVersion")){
...
}

else if (localName.equalsIgnoreCase("vsDataType")){
...
}
}

In the startElement() method example, start tags are checked using the tag information that comes as a parameter. The localName parameter used here refers to the name of the tag. The values of the tag can be reached with the atts parameter.

Characters

If there is data in an element in the XML file, the characters() method is called. With this method, parameters in the tag can be accessed.

public void characters(char ch[], int start, int length) throws SAXException {
String value = new String(ch, start, length);

if (inVsDataType){
...
}
else if (inMo && inParameter) {
...
}
}

End Element

The endElement() method is called when SAX Parser sees a closing tag while it is moving through the file as a stream. As in the startElement() method, the tag information is sent as a parameter in this method. The endElement() method example is as follows.

public void endElement(String nameSpaceURI, String localName, String qName) {      if (localName.equalsIgnoreCase("SubNetwork")){
...
}

else if (localName.equalsIgnoreCase("VsDataContainer")){
...
} else if (localName.equalsIgnoreCase("vsDataFormatVersion")){
...
} else if (localName.equalsIgnoreCase("vsDataType")){
...
}
}

Finally, let’s create a main method in the Parser class to run the My_Handler class we created.

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;public class Parser{

public static void main (String[] args)
try {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
My_Handler handle = new My_Handler();
parser.parse(("/data.xml"), handle);

}
catch (ParserConfigurationException e) {
pr.ErrorMessage = new StringBuilder(e.getMessage());
} catch (SAXException e) {
pr.ErrorMessage = new StringBuilder(e.getMessage());
} catch (IOException e) {
pr.ErrorMessage = new StringBuilder(e.getMessage());
}
return pr;
}
}

Above, within the main function SaxParserFactory defines an API to parse the XML file. Thus, the SAX Parser object is obtained with the created API. The SAX Parser object takes the file and the created handle as parameters. Thus, the parser process starts.

With the SAX Parser library, large XML files such as 20 GB can be parsed in a short time. In doing this, there is no need to store the entire XML file in memory. Thus, speed and cost are gained in the parsing process.

Good Luck 😊