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 :
160
The org.xml.sax.HandlerBase Handler
All Known Implemented Interfaces: org.xml.sax.DocumentHandler,
org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, and
org.xml.sax.ErrorHandler
Members
Signature
Public methods
void
characters(char[] ch, int start, int length)
void
endDocument()
void
endElement(String name)
void
error(SAXParseException e)
void
fatalError(SAXParseException e)
void
ignorableWhitespace(char[] ch, int start, int length)
This helper class provides empty implementation of all SAX 1.0 core
handler interfaces, and can be extended to allow addition of handlers by
overriding methods with application-defined behavior. This class was defined in
SAX 1.0, and is now deprecated. The org.xml.sax.helpers.DefaultHandler
class should be used instead of HandlerBase for SAX 2.0 implementations.
To use JAXP with SAX-compliant
parser, your only task is to extend the HandlerBase class and implement the
callbacks desired for your application. The alternative in SAX 2.0 is doing the
same with DefaultHandler. An instance of your extension class then becomes the
core argument for most of the JAXP methods that deals with SAX.
Here’s the typical SAX rundown:
Create a
SAXParser instance using a
specific vendor’s parser implementation.
Register
callback implementations (by using
a class that extends HandlerBase)
Start parsing
and relax as your callback implementations are fired off.
You only
need to implement those callback methods of HandlerBase, that needed for your
application.
The SAX component of JAXP
provides a simple means to do all of this. Without JAXP, a SAX parser instance
either must be instantiated directly from a vendor class ( such as
org.apache.xerces.parsers.SAXParser), or it must use a SAX helper class called
ParserFactory ( the SAX 1.0 version of SAX 2.0’s XMLReaderFactory).
The JAXP provides a better alternative.
It allows you use the vendor class as a parser through a java system property. Please
refer your vendor’s documentation for more details regarding the parser
factory.
Method Overviews
All most all methods in this class are important. It depends upon your
applications needs regarding what are the methods you need to implement in your
parser mechanism. Some of the methods are generally used across all SAX
parsers. I have listed the methods, those are commonly used across all SAX
parsers.
Example of startDocument()
/**
* This method receives notification, at the
start of the XML Document.
*/
public void startDocument()
{
System.out.println("Start of the
Document");
}
Example of startElement()
/**
* This
method receives notification at the start of each element of XMl Document for
SAX parser.
* @param
name name of the element
* @param
attributes attributes associated with the element.
*/
public void startElement(String name, AttributeList attributes)
{
System.out.println("Start of the element and Element Name
:"+name);
for (int
i = 0; i < attributes.getLength(); i++) {
String
attrname = attributes.getName(i);
String
attrtype = attributes.getType(i);
String
attrvalue = attributes.getValue(i);
System.out.println("Attribute
Name :"+attrname);
System.out.println("Attribute Type :"+attrtype);
System.out.println("Attribute Value :"+attrvalue);
}
}
Example of characters()
/**
* Receive
notification of character data inside an element.
* @param
ch The characters.
* @param
start The start position in the character array.
* @param
length The number of characters to use from the character array.
*/
public void
characters(char[] ch, int start, int length)
{
System.out.println(" Character :"+new String(ch,
start,length));