Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

JAXP - SAX

1
Objectives
2

• Explain SAX
• Discuss JAXP
• Discuss the working of
– JAXP with SAX
SAX - Simple API for XML
3
SAX - Package
4

• SAX 2.0 API packages


– org.xml.sax – Provides the core SAX API.
– org.xml.sax.helpers – Interfaces to SAX2 facilities that
conformant SAX drivers would not necessarily support.

• SAX 2.0 API extension package

– org.xml.sax.ext – “helper” classes, including support for


bootstrapping SAX-based applications.
SAX - Package
5

Package Name Description

Contains basic classes, interfaces and exceptions necessary to parse


documents. The interfaces needed to create handlers for various events
org.xml.sax are part of this package.
Some SAX APIs are deprecated to encourage integration of namespace
awareness into designs of new applications and into maintenance of
existing infrastructure.

Contains additional classes that can simplify coding. Adapters and


Factory classes are part of this package.
org.xml.sax.helpers Standardized extension to SAX2. It is designed both to allow SAX
parsers to pass certain types of information to applications, and to serve
as a simple model for other SAX2 parser extension package.

Contains two handler interfaces for capturing declaration and lexical


org.xml.sax.ext events.
Contains “helper” classes, including support for bootstrapping SAX-
based applications.
ContentHandler interface
6

• Main interface implemented by most SAX applications.


• Application can implement this interface and register an
instance with the SAX parser using the
setContentHandler() method.
• It is similar to the deprecated interface
DocumentHandler in SAX 1.0.
ContentHandler interface
7

Method Description
void startDocument() It receives notification of the beginning of the
document
void endDocument() It receives notification of the end of the
document
void startElement(String uri,
String name, String qname, It receives the notification for the beginning of
an element
Attributes attr)
void endElement(String uri, It receives notification of the end of an
String name, String qname) element.
characters(char ch[ ], int
start, int length) It receives the notification for character data

void ignorableWhitespace(char It receives the notification for the presence of


ch[ ], int start, int length) ignorable
white space in the element content.
ContentHandler interface
8

Methods Description
void processingInstruction(
String target, string data) It receives notification of a processing instruction.
void It receives an object that locates the origin of SAX
setDocumentLocator( Locator document events. The Locator object can be used to get
information about the location of the event, such as the
locator)
line number and the column position
void It begins the scope of a prefix-URI Namespace
startPrefixMapping( String mapping.
This method will be invoked before the corresponding
prefix, String uri)
startElement event.
void
endPrefixMapping(String It ends the scope of a prefix-URI mapping and this
event occurs after the corresponding endElement event.
prefix)
void skippedEntity(String It receives notification of a skipped entity and this
name) method is invoked by the Parser for each entity that is
skipped.
XMLReader interface
9

• It allows an application to set and query features and


properties in the parser, to register event handlers for
document processing and to begin parsing of a document.

• It replaces the deprecated Parser interface in SAX 1.0 by


adding Namespace support, which is required for high-level
XML standards and adding a standard way for querying and
setting features and properties.
XMLReader interface
10

• Commonly used methods are:


Method Description
ContentHandler
getContentHandler() Return the current content handler.

EntityResolver
getEntityResolver() Return the current entity resolver.

ErrorHandler getErrorHandler() Return the current error handler.


Object
getProperty(String name) Look up the value of a property.

DTDHandler getDTDHandler() Return the current DTD handler

boolean getFeature(String name) Look up the value of a feature specified as a


fully qualified URI in the variable name.

void parse(InputSource input) Parse an XML document.


XMLReader interface
11

void parse(String systemId) Parse an XML document from a system


identifier (URI).
void
setContentHandler(ContentHandler Allow an application to register a content
event handler.
handler)
void
setErrorHandler(ErrorHandler Allow an application to register an error event
handler.
handler)
void setProperty(String name,
Object value) Set the value of a property.
void setDTDHandler (DTDHandler Allow an application to register a DTD event
handler) Handler.
void setEntityResolver Allow an application to register an entity
(EntityResolver resolver) resolver.
void setFeature (String name, Set the state of a feature which is a fully
Boolean value) qualified URI.
DefaultHandler Class
12

• Default base class for SAX2 event handlers; it provides default


implementations for all of the callbacks in the four core SAX2 handler classes:
– EntityResolver
– DTDhandler
– ContentHandler
– ErrorHandler
• The org.xml.sax.helpers.DefaultHandler class provides empty
implementations of each method of each interface.
• The important feature of SAX 2.0 is that it adds namespace support.
DefaultHandler Class
13

Method Description

void notationDecl (String name, Receives notification of notation


String publicId, String systemId) declaration.

InputSource resolveEntity (String


publicId, String systemId) Resolves an external entity.

void unparsedEntityDecl(String name,


String publicId, String systemId, Receives notification of an unparsed
String notationName) entity declaration.

void warning(SAXParseException e) Receives notification of a parser


warning.

void error(SAXParseException e) Receives notification of a recoverable


parser error
JAXP - Java API for XML Processing
14
JAXP with SAX
15
JAXP with SAX
16
JAXP with SAX
17

• JAXP 1.2 provides two options:


– Set the namespace
– Turn on the validation
• The setNamespaceAware()method is used to set the namespace.
• The setValidating()method is used to turn on the validation.
• The newSAXParser() method returns a ready to use instance of
the JAXP SAXParser class.
Q&A
18

You might also like