Parse XML file with SAX
With this example we are going to demonstrate how to parse an XML file with SAX. Simple API for XML (SAX) is an event-driven, serial-access mechanism for accessing XML documents. It is frequently used by servlets and network-oriented programs that need to transmit and receive XML documents, because it is the fastest and least memory-intensive mechanism that is currently available for dealing with XML documents, other than the Streaming API for XML (StAX). We have created a Class, ParseXMLFileWithSAX
that is a handler that extends the DefaultHandler and overrides its startElement(String uri, String localName, String qName, Attributes attributes)
and endElement(String uri, String localName, String qName)
API methods. The basic steps of the example are described below:
- Create a new instance of the
ParseXMLFileWithSAX
class. - Obtain a new instance of a SAXParserFactory, that is a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents.
- Set the parser produced by this code so as not to validate documents as they are parsed, using
setValidating(boolean validating)
API method of SAXParserFactory with validating set to false. - Create a new instance of a SAXParser, using
newSAXParser()
API method of SAXParserFactory. - Use
parse(File f, DefaultHandler dh)
API method of SAXParser to parse the content of the specified XML file, using the specified handler of the example. - The
ParseXMLFileWithSAX
class of the example overridesstartElement(String uri, String localName, String qName, Attributes attributes)
andendElement(String uri, String localName, String qName)
API methods of DefaultHandler. For example it can append to a buffer the attributes of the Element and when it reaches to the end of the element it can print the results.
Let’s take a look at the code snippet that follows:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package com.javacodegeeks.snippets.core; import java.io.File; import java.util.LinkedList; import java.util.List; 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 ParseXMLFileWithSAX extends DefaultHandler { private StringBuffer buffer = new StringBuffer(); private static String responseCode; private static String date; private static String title; private static Currency currency; private static Rates rates; public static void main(String[] args) throws Exception { DefaultHandler handler = new ParseXMLFileWithSAX(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating( false ); SAXParser parser = factory.newSAXParser(); parser.parse( new File( "in.xml" ), handler); System.out.println( "Response Code:" + responseCode); System.out.println( "Date:" + date); System.out.println( "Title:" + title); System.out.println( "Rates:" ); for (Currency curr : rates.currencies) { System.out.println( "tCode:" + curr.code + " - Rate:" + curr.rate); } } private static class Currency { public String code; public String rate; } private static class Rates { public List<Currency> currencies = new LinkedList<Currency>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { buffer.setLength( 0 ); if (qName.equals( "response" )) { responseCode = attributes.getValue( "code" ); } else if (qName.equals( "date" )) { date = "" ; } else if (qName.equals( "title" )) { title = "" ; } else if (qName.equals( "rates" )) { rates = new Rates(); } else if (qName.equals( "currency" )) { currency = new Currency(); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals( "date" )) { date = buffer.toString(); } else if (qName.equals( "title" )) { title = buffer.toString(); } else if (qName.equals( "currency" )) { rates.currencies.add(currency); } else if (qName.equals( "code" )) { currency.code = buffer.toString(); } else if (qName.equals( "rate" )) { currency.rate = buffer.toString(); } } public void characters( char [] ch, int start, int length) { buffer.append(ch, start, length); } } |
Input:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | <? xml version = "1.0" encoding = "UTF-8" ?> < response code = "200" > < date >2008-11-07</ date > < title >Exchange rates for 2008-11-07</ title > < rates > < currency > < code >EUR</ code > < rate >1.220</ rate > </ currency > < currency > < code >USD</ code > < rate >1.275</ rate > </ currency > </ rates > </ response > |
Output:
Response Code:200
Date:2008-11-07
Title:Exchange rates for 2008-11-07
Rates:
Code:EUR - Rate:1.0
Code:USD - Rate:1.275600