Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
263
The org.xml.sax.XMLReader Interface
Sub Interfaces: org.xml.sax.XMLFilter
Implementing classes: org.xml.sax.helpers.ParserAdapter,
org.xml.sax.helpers.XMLFilterImpl
Members
Signature
Public methods
ContentHandler
getContentHandler()
DTDHandler
getDTDHandler()
EntityResolver
getEntityResolver()
ErrorHandler
getErrorHandler()
boolean
getFeature(String name)
Object
getProperty(String name)
void
parse(InputSource input)
void
parse(String systemId)
void
setContentHandler(ContentHandler handler)
void
setDTDHandler(DTDHandler handler)
void
setEntityResolver(EntityResolver resolver)
void
setErrorHandler(ErrorHandler handler)
void
setFeature(String name, boolean value)
void
setProperty(String name, Object value)
Overview
This is the core interface that defines parsing
behaviour in SAX2.0 is vendors XML parsing software must include at least one
implementation of this interface. It replaces the SAX1.0 Parser interface by
adding support for namespaces in a Document’s elements and attributes. In
additing to providing an entry into parsing (with either a system ID or
InputSource as input), it allows registering of the various handler interfaces
that SAX2.0 provides. The features and properties available to a SAX parser
implementation are also set through this interface.
Method Overviews
All most all methods in this
interface are important. The implementation depends upon application
requirement. I tried to give examples of methods that have come across during
my execution and are also generally used across parsing specific applications.
Example of parse(InputSource input)
This method parses an XML document. The application
can use this method to instruct the XML reader to begin parsing an XML document
from any valid input source (a character stream, a byte stream, or a URI). Applications
may not invoke this method while a parse is in progress (they should create a
new XMLReader instead for each nested XML document). Once a parse is complete,
an application may reuse the same XMLReader object, possibly with a different
input source. During the parse, the XMLReader will provide information about
the XML document through the registered event handlers. This method is
synchronous: it will not return until parsing has ended. If a client
application wants to terminate parsing early, it should throw an exception.
This
method throws
org.xml.sax.SAXException
: This occurs when any error occurs during parsing.
java.io.IOException
: This occurs when any error occurs while reading an XML document from byte
stream, character stream.
InputSource source = new InputSource(systemId);
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(source);
Example of parse(String systemId)
Parse an XML document from a system identifier
(URI). This method is a shortcut for the common case of reading a document from
a system identifier. If the system identifier is a URL, it must be fully
resolved by the application before it is passed to the parser.
This
method throws :
org.xml.sax.SAXException
: This occurs when any error occurs during parsing.
java.io.IOException
: This occurs when any error occurs while reading an XML document from byte
stream, character stream.
ContentHandlerImpl contentHandler = new
ContentHandlerImpl();
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(systemId);
Example of setContentHandler(ContentHandler handler)
Allow an application to register a content event
handler. If the application does not register a content handler, all content
events reported by the SAX parser will be silently ignored. Applications may
register a new or different handler in the middle of a parse, and the SAX
parser must begin using the new handler immediately.
This
method throws :
java.lang.NullPointerException : If
the handler argument is null.
ContentHandlerImpl contentHandler =
new ContentHandlerImpl(); //this class implements ContentHandler interface
xmlReader.setContentHandler(contentHandler);
Example of setErrorHandler(ErrorHandler handler)
Allow an application to register an error event handler.
If the application does not register an error handler, all error events
reported by the SAX parser will be silently ignored; however, normal processing
may not continue. It is highly recommended that all SAX applications implement
an error handler to avoid unexpected bugs. Applications may register a new or
different handler in the middle of a parse, and the SAX parser must begin using
the new handler immediately.
This
method throws :
java.lang.NullPointerException : If
the handler argument is null.
ErrorHandlerImpl errorHandler = new
ErrorHandlerImpl(); //This class implements ErrorHandler interface.
xmlReader.setErrorHandler(errorHandler);
Example of setDTDHandler(DTDHandler handler):
Allow an application to register a DTD event
handler. If the application does not register a DTD handler, all DTD events
reported by the SAX parser will be silently ignored. Applications may register
a new or different handler in the middle of a parse, and the SAX parser must
begin using the new handler immediately.
This
method throws :
java.lang.NullPointerException : If
the handler argument is null.
DTDHandlerImpl dtdHandler = new DTDHandlerImpl();//This
class implements DTDHandler impl.