Java SAX parsing error handling

Today we are going to talk about error handling while using SAX parser in Java. Since SAX parser is a stream parser you can handle errors while the parser is working. Here is sample project structure.

Java SAX parsing error - Eclipse Project

Java SAX parsing error – Eclipse Project

Parsing handler class implementation is simple. This implementation displays tags and the content being parsed. This class contains error and fatalError methods that are called if an error occurs.

error – this method is called whenever a minor error is detected. For example XML document doesn’t validate against the schema.
fatalError – this method is called when a fatal error is detected and further document processing is impossible. For example when XML file is broken.

CustomParseHandler.java

package com.itcuties.java;

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

public class CustomParseHandler extends DefaultHandler {
	
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    	System.out.print("<"+qName+">");
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
    	System.out.print("</"+qName+">");
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
       System.out.print("\t" + new String(ch, start, length));
    }
 
    @Override
    public void error(SAXParseException e) throws SAXException {
    	System.out.println("Error: " + e.getMessage());
    }
 
    @Override
    public void fatalError(SAXParseException e) throws SAXException {
    	System.out.println("Fatal error: " + e.getMessage());
    }
    
}

Here is the code that starts the parsing process. The SAXParserFactory.setNamespaceAware(true) and SAXParserFactory.setValidating(true) methods are used to allow the SAXParser to validate XML documents.

SaxParserRunner.java

package com.itcuties.java;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SaxParserRunner {
	public static void main(String[] args) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setValidating(true);
			// Using factory we create a new SAX Parser instance
			SAXParser saxParser = factory.newSAXParser();
			
			// We need the SAX parser handler object
			CustomParseHandler handler = new CustomParseHandler();
			// We call the method parsing our RSS Feed
			// Parse valid XML
			saxParser.parse(new File("input/valid-file.xml"), handler);
			
			// Parse error XML
			//saxParser.parse(new File("input/error-file.xml"), handler);
			
			// Parse fatal error XML
			//saxParser.parse(new File("input/fatalerror-file.xml"), handler);
			
		} catch (ParserConfigurationException pce) {
			//pce.printStackTrace();
			// FIXME: Do nothing at the moment 
		} catch (SAXException se) {
			//se.printStackTrace();
			// FIXME: Do nothing at the moment 
		} catch (IOException ioe) {
			//ioe.printStackTrace();
			// FIXME: Do nothing at the moment 
		}
	}
}

The output of running this code with the valid XML is this.

valid-file.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book>
        <title>Thinking in Java</title>
        <author>Bruce Eckel</author>
        <description>
            Thinking in Java (ISBN 978-0131872486) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.
        </description>
    </book>
    <book>
        <title>Lord of the Rings</title>
        <author>J. R. R. Tolkien</author>
        <description>
            The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.
        </description>
    </book>
    <book>
        <title>The C Programming Language</title>
        <author>Brian W. Kernighan and Dennis M. Ritchie</author>
        <description>
            The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of the C programming language and is still widely read and used today.
        </description>
    </book>
</library>

output:

<library>	
    <book>	
        <title>	Thinking in Java</title>	
        <author>	Bruce Eckel</author>	
        <description>	
            Thinking in Java (ISBN 978-0131872486) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.
        </description>	
    </book>	
    <book>	
        <title>	Lord of the Rings</title>	
        <author>	J. R. R. Tolkien</author>	
        <description>	
            The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.
        </description>	
    </book>	
    <book>	
        <title>	The C Programming Language</title>	
        <author>	Brian W. Kernighan and Dennis M. Ritchie</author>	
        <description>	
            The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of the C programming language and is still widely read and used today.
        </description>	
    </book>	
</library>

The output of running this code with the XML which doesn’t validate against the schema is this.

error-file.xml

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://maven.apache.org/POM/4.0.0" 
    	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <book>
        <title>Thinking in Java</title>
        <author>Bruce Eckel</author>
        <description>
            Thinking in Java (ISBN 978-0131872486) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.
        </description>
    </book>
    <book>
        <title>Lord of the Rings</title>
        <author>J. R. R. Tolkien</author>
        <description>
            The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.
        </description>
    </book>
    <book>
        <title>The C Programming Language</title>
        <author>Brian W. Kernighan and Dennis M. Ritchie</author>
        <description>
            The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of the C programming language and is still widely read and used today.
        </description>
    </book>
</library>

output:

Error: Document is invalid: no grammar found.
Error: Document root element "library", must match DOCTYPE root "null".
<library>	
    <book>	
        <title>	Thinking in Java</title>	
        <author>	Bruce Eckel</author>	
        <description>	
            Thinking in Java (ISBN 978-0131872486) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.
        </description>	
    </book>	
    <book>	
        <title>	Lord of the Rings</title>	
        <author>	J. R. R. Tolkien</author>	
        <description>	
            The Lord of the Rings is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.
        </description>	
    </book>	
    <book>	
        <title>	The C Programming Language</title>	
        <author>	Brian W. Kernighan and Dennis M. Ritchie</author>	
        <description>	
            The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of the C programming language and is still widely read and used today.
        </description>	
    </book>	
</library>

The output of running this code with the broken XML file is this.

fatalerror-file.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book>
        <title>Thinking in Java</title>
        <author>Bruce Eckel</author>
        <description>
            Thinking in Java (’ISBN 978-0131872486’) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.

		<!-- Missing ending </description> tag here -->

    </book>
    <book>
        <title>Lord of the Rings</title>
        <author>J. R. R. Tolkien</author>
        <description>
            “The Lord of the Rings“ is an epic high fantasy novel written by English philologist and University of Oxford professor J. R. R. Tolkien. The story began as a sequel to Tolkien's 1937 children's fantasy novel The Hobbit, but eventually developed into a much larger work. It was written in stages between 1937 and 1949, much of it during World War II.
        </description>
    </book>
    <book>
        <title>The C Programming Language</title>
        <author>Brian W. Kernighan and Dennis M. Ritchie</author>
        <description>
            The C Programming Language is a well-known programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as co-designed the Unix operating system with which development of the language was closely intertwined. The book was central to the development and popularization of the C programming language and is still widely read and used today.
        </description>
    </book>
</library>

output:

<library>	
    <book>	
        <title>	Thinking in Java</title>	
        <author>	Bruce Eckel</author>	
        <description>	
            Thinking in Java (’ISBN 978-0131872486’) is a book about the Java programming language, written by Bruce Eckel and first published in 1998. Prentice Hall published the 4th edition of the work in 2006. The book represents a print version of Eckel’s "Hands-on Java" seminar.

			

    Fatal error: The element type "description" must be terminated by the matching end-tag "</description>".

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW