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 :
177
Overview of Content Handlers
There are four
core handler interfaces defined by SAX 2.0: org.xml.sax.ContentHandler,
org.xml.sax.ErrorHandler, org.xml.sax.DTDHandler, and
org.xml.sax.EntityResolver. In this scenario we discuss ContentHandler, which
allows standard data-related events within an xml document to be handled, and
take a first look at ErrorHandler, which receives notifications from the parser
when errors in the xml data are found. DTDHandler will be examined in later
discussion.
The org.xml.sax.ContentHandler Interface
All Known Subinterfaces: javax.xml.transform.sax.TemplatesHandler,
javax.xml.transform.sax.TransformerHandler.
All Known Implementing Classes:
org.xml.sax.helpers.DefaultHandler,
org.xml.sax.helpers.XMLFilterImpl, org.xml.sax.helpers.XMLReaderAdapter
This is a public interface so
user needs to create a class wrapped on this interface. Receive notification of
the logical content of a document. This is the main interface that most SAX
applications implement: if the application needs to be informed of basic
parsing events, it implements this interface and registers an instance with the
SAX parser using the setContentHandler method of org.xml.sax.XMLReader or
parse(inputsource/file, ContentHandler) method of SAXParser. The parser uses
the instance to report basic document-related events like the start and end of
elements and character data.
The order of events in this
interface is very important, and mirrors the order of information in the
document itself. For example, all of an element's content (character data,
processing instructions, and/or subelements) will appear, in order, between the
startElement event and the corresponding endElement event.
This interface is similar to the
now-deprecated SAX 1.0 DocumentHandler interface, but it adds support for
Namespaces and for reporting skipped entities (in non-validating XML
processors).
Method Overview
Most of the methods in this
interface are important and depends upon the requirement of your application.
To know more details, please refer the example with method description.
Example:
/**
* <p>Title: MyContentHandler.java</p>
* <p>Description: MyContentHandler implements the SAX
ContentHandler interface
* and defines callback
behaviour for the SAX callbacks associated with
* an XML documents
content.</p>
*/
import org.xml.sax.Attributes;
import
org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class MyContentHandler
implements ContentHandler{
/**
* This reports character data within an element.
* @param ch
* @param start
* @param length
*/
public void characters(char[] ch, int start, int length){
}
/**
* This indicates the end of a Document parse-this occurs after all
callbacks
* in all SAX Handlers.
*/
public void endDocument() {
}
/**
* Indicates the end of an element </elementname> is reached. Note
that the parser
* does not distinguish between empty elements and non empty elements, so
this
* occurs uniformly.
* @param namespaceURI
* @param localName
* @param qName
*/
public void endElement(String namespaceURI, String localName,
String qName){
}
/**
* This indicates the end of a prefix mapping, when the namespace
reported in a
* #startPrefixMapping call back
is no longer available.
* @param prefix
*/
public void endPrefixMapping(String prefix) {
}
/**
* This reports whitespace that can be ignored in the originating
document.
* This is typically invoked only when validation is occurring in the
parsing
* process.
* @param ch
* @param start
* @param length
*/
public void ignorableWhitespace(char[] ch, int start, int length){
}
/**
* This indicates that a processing instruction (other tahn the xml
declaration)
* has been encountered.
* @param target
* @param data
*/
public void processingInstruction(String target, String data){
}
/**
* Provides references to Locator which provides information about where
in
* a document callbacks occur.
* @param locator
*/
public void setDocumentLocator(Locator locator){
}
/**
* This reports an entity that is skipped by the parser. This should only
occur
* for non-validating parsers, and then is still implementation-dependent
behaviour.
* @param name
*/
public void skippedEntity(String name){
}
/**
* This indicates the start of a document parse-this precedes all
callbacks
* in all SAX handlers with the sole exception of setDocumentLocator.
*/
public void startDocument(){
}
/**
* This reports the occurence of an actual element. It includes the
elements
* attributes, with the exception of XML vocabulary specific attributes,
such as
* xmlns:[namespace prefix] and xsi:[schemaLocation]
* @param namespaceURI
* @param localName
* @param qName
* @param atts
*/
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts){
}
/**
* This indiacates the beginning of an xml Namespace prefix mapping.
Although
* this typically occurs within the root element of an XML document, it
can occur
* at any point within the document. Note that prefix mappings of an
element
* triggers this callback before the callback for the actual element
itself
* #startElement occurs.
* @param prefix
* @param uri
*/
public void startPrefixMapping(String prefix, String uri){