• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Parse XML file using SAX Parser?

October 27, 2013 //  by Krishna Srinivasan//  Leave a Comment

In my previous article I had written the example program for parsing a simple XML file using the DOM parser. As I have mentioned in the earlier posts, DOM and SAX are the two popular parser used for reading and manipulating the XML files. If you use any of the XML utility like JAXB for Java / XML binding, they are internally using any of these parser. DOM and SAX are the core APIs for reading the XML files. Both are working in the different way.

  1. DOM loads the entire XML file into meorty and then retrives the XML elements. It is good for the small XML files. If the XML file is huge in size, it will impact the performance and consumes lot of memory.
  2. SAX parser uses the event driven model to find an element. It traverses the entire XML file to find the elements.  SAX parser is faster and less memory then a DOM parser.

SAX uses the callback functions the class org.xml.sax.helpers.DefaultHandler for reading the XML elements. The following are the callback functions:

  1. startDocument and endDocument: This mthods are are called at the start and end of the document.
  2. startElement and endElement: This methods are called at the time when parser reading the start tag and end tag for each elements.
  3. characters: This method is called when it finds text content inside the start and end tag of an element.

Look at the example. Before that look how the folder structure for this example is setup. Keep the XML at the root of the project directory.

XML SAX Parser

1. Create A Simple XML File

employee.xml

<?xml version="1.0"?>
<company>
	<employee id="001">
		<firstname>Krishna</firstname>
		<lastname>Srinivasn</lastname>
	</employee>
	<employee id="002">
		<firstname>Muthu</firstname>
		<lastname>Kumar</lastname>
	</employee>
</company>

2. SAX Parser For Parsing The XML File

JavaSAXParser.java

package javabeat.net.xml;

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 JavaSAXParser extends DefaultHandler {
	public static void main(String args[]) {
		JavaSAXParser xmlParser = new JavaSAXParser();
		xmlParser.parseXmlUsingSAX();
	}

	public void parseXmlUsingSAX() {
		try {
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			SAXParser saxParser = saxParserFactory.newSAXParser();
			DefaultHandler defaultHandler = new DefaultHandler() {
				String firstNameTag = "close";
				String lastNameTag = "close";

				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {

					if (qName.equalsIgnoreCase("FirstName")) {
						firstNameTag = "open";
					}
					if (qName.equalsIgnoreCase("lastName")) {
						lastNameTag = "open";
					}
				}

				public void characters(char ch[], int start, int length)
						throws SAXException {

					if (firstNameTag.equals("open")) {
						System.out.println("First Name : "
								+ new String(ch, start, length));
					}
					if (lastNameTag.equals("open")) {
						System.out.println("Last Name : "
								+ new String(ch, start, length));
					}
				}

				public void endElement(String uri, String localName,
						String qName) throws SAXException {

					if (qName.equalsIgnoreCase("firstName")) {
						firstNameTag = "close";
					}
					if (qName.equalsIgnoreCase("lastName")) {
						lastNameTag = "close";
					}
				}
			};
			saxParser.parse("employees.xml", defaultHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

The output for the above program will be:

SAX Parser Output

I hope the above example helped you to understand the SAX implementation. If you have any questions, please write it in the comments section.

Category: XMLTag: XML

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to Parse XML file using DOM Parser?
Next Post: How To Convert Properties File Into XML File Using Java? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact