Here i will show a simple example to demonstrate how to read a XML file in Java with SAX parser.
The SAX parser is created by the javax.xml.parsers.SAXParserFactory. SAX parser is work differently with DOM parser, the SAX parser does not load any XML document into memory and create some object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.
Remember
SAX Parser is faster and uses less memory than DOM parser.
If you want to compare different implementation between DOM and SAX, Please visit here for How to read XML file in Java – (DOM Parser)
Here are 3 important methods in SAX parser
1) startDocument() and endDocument() – methods called at the start and end of an XML document.
2) startElement() and endElement() – methods called at the start and end of a document element.
3) characters() – method called with the text contents in between the start and end tags of an XML document element.
Let’s start to see a simple SAX parser example.
Tutorial …
1. Create a XML file
Create a simple XML file as following
<?xml version="1.0"?> <company> <staff> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
2. Create a Java file
Create a SAX parser Java file as following
package com.mkyong.test; 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 ReadXMLFileSAX { public static void main(String argv[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean bfname = false; boolean blname = false; boolean bnname = false; boolean bsalary = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("Start Element :" + qName); if (qName.equalsIgnoreCase("FIRSTNAME")) { bfname = true; } if (qName.equalsIgnoreCase("LASTNAME")) { blname = true; } if (qName.equalsIgnoreCase("NICKNAME")) { bnname = true; } if (qName.equalsIgnoreCase("SALARY")) { bsalary = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("End Element :" + qName); } public void characters(char ch[], int start, int length) throws SAXException { if (bfname) { System.out.println("First Name : " + new String(ch, start, length)); bfname = false; } if (blname) { System.out.println("Last Name : " + new String(ch, start, length)); blname = false; } if (bnname) { System.out.println("Nick Name : " + new String(ch, start, length)); bnname = false; } if (bsalary) { System.out.println("Salary : " + new String(ch, start, length)); bsalary = false; } } }; saxParser.parse("c:\\file.xml", handler); } catch (Exception e) { e.printStackTrace(); } } }
3. Run it
Execute the Java program and see the output below
Start Element :company Start Element :staff Start Element :firstname First Name : yong End Element :firstname Start Element :lastname Last Name : mook kim End Element :lastname Start Element :nickname Nick Name : mkyong End Element :nickname Start Element :salary Salary : 100000 End Element :salary End Element :staff Start Element :staff Start Element :firstname First Name : low End Element :firstname Start Element :lastname Last Name : yin fong End Element :lastname Start Element :nickname Nick Name : fong fong End Element :nickname Start Element :salary Salary : 200000 End Element :salary End Element :staff End Element :company
This example may encounter some exceptions for UTF-8 XML file, please read this article about how to read the XML “UTF-8″ file in SAX



[...] You can compare the syntax with SAX and DOM parser in the following two examples – How to read XML file in Java – (SAX Parser) – How to read XML file in Java – (DOM [...]
[...] Written on December 17, 2009 at 6:04 am by mkyong Here’s my previous article about how to read XML file in Java SAX Parser. The previous example is working fine to parse the plain text (ANSI) XML file, however, it may [...]
[...] This is the Java’s example i used to read the XML file with SAX. [...]
while i try to compile the sax parser code i got the following error:
org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at com.mypack.ReadXMLFileSAX.main(ReadXMLFileSAX.java:99)
This may cause by the invalid XML content. please read
http://www.mkyong.com/java/sax-error-content-is-not-allowed-in-prolog/
Or, can you provide me your XML file?
[...] If you want to compare different implementation between DOM and SAX, Please visit here for How to read XML file in Java – (SAX Parser) [...]