How to read XML using SAX parser

In the previous article we talked about DOM parser and provided different examples for parsing and reading elements of an XML document. SAX parser is yet another XML parser provided by JDK which parses documents in a more optimized and faster way.

SAX parser doesn’t load the whole document into the memory, however it parses the document line by line and provides callback operations to the developer in order to handle each read tag separately.

If you’re interested in STAX or DOM parser, please refer to these tutorials: STAX parserDOM parser.

1- Students.xml

Consider we have the following Students.xml file:

2- Student.java

For mapping purposes, we create Student.java for populating each student element inside Students.xml:

3- Define SAX handler

In this section, we’re going to parse students.xml and populate a List of Student objects out of it.

SAX parses documents using a handler. In order to define our own customized handler, we define a class called SAXHandler as the following:

Following is a brief description for the above code snippet:

  1. startDocument(): This method is called when the parser starts parsing the document.
  2. endDocument(): This method is called when the parser ends parsing the document.
  3. startElement(): This method is called when the parser starts parsing a specific element inside the document.
    1. qName: refers to the element or tag name.
    2. attributes: refers to the attributes linked to the element.
    3. In the above example, we’re instantiating a new Student object whenever the parser starts parsing a ‘student’ element.
  4. endElement(): This method is called when the parser ends parsing a specific element inside the document.
    1. qName: refers to the element or tag name
    2. In the above example, we’re adding the already instantiated Student object to students list whenever we reach the end of student element. If the ending element is id or name, then we set the id and name of the current student object.
  5. characters(): This method reads the text value of the currently parsed element. We’re saving the text value in a class field called elementValue so that we access it inside endElement().
  6. getStudents(): This method exposes the populated list of Student objects so that caller classes can use it.

4- Parse students.xml

Now we create our main class named as ReadXMLWithSAX which parses students.xml using SAXParser.

After running the above main method, we get the following output:

5- Source Code

You can download the source code from this repository: Read-XML

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar