Event-based with Java's class SAXParser
The purpose of this assignment was to:
- Learn how to use Java's class SaxParser to access and parse XML Documents.
- Learn the strength and weakness of class SAXParser
The description and my solution of the Event-based with SAXparser - assignment can be viewed at the bottom of this page.
What is a SAXParser?
The name SAX is short for "Simple API for XML".
SAX is a serial access parser API for XML used by java programmers to provide a mechanism for accessing data from an XML document.
A parser which implements a SAX Parser handles XML information as a unidirectional stream of data. That is, previously accessed data cannot be re-read without reparsing the document.
The SAX parser is implemented as an event-driven model in which the programmer provides callback methods which are invoked by the parser as part of it's traversal of the XML document.
Strengths and weaknesses of SAX
Strengths:
The strength of implementing a parser with SAX is that it can parse gigabytes worth of XML documents without hitting resource limits. This because it does not try to create a representation of entire XML documents in memory; as for example a DOM representation does.
If, for example, one need to grab text of a few elements from a document, or if one need to extract elements from a large stream of XML, this can be done efficiently with SAX. Because of this design, the SAX implementation is generally considered faster and less resource heavy.In short: SAX is lightweight and event-driven.
Weaknesses:
The main weakness of SAX is that it's operating on a tag level without any help from the parser to maintain the context.
Also, SAX is considered kind of complex since the lack of a document representation leaves a programmer with the challenge of serializing and manipulating XML documents at a low level.
Assignment Description
Create a java application that implements a XML Parser using SAX. The application should be able to be executed from the command prompt like this:
java SAXChecker <xml-document> <validate>
If validate is set to true the application should try and validate the document(against a DTD or XML Schema), otherwise - if set to false - it should check if the document is well-formed(has correct XML syntax).
for example:
(validation is on)
java SAXChecker myxmldoc.xml true
or
(validation is off)
java SAXParser myxmldoc.xml false
Try SAXParser application on the cv-template.xml from the DTD and XML, Part 1-2 -assignment to check it for well-formedness and validness.
My solution, Assignment Files
-
SAXChecker.java
The main-method class. -
SAXErrorHandler.java
Takes care of exceptions thrown during the parsing of non-well-formed or non-validating XML documents.
