License:
Version:
Authors:
Acknowledgements:
This is the main interface that most SAX applications
implement:
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).
Implementors should note that there is also a
ContentHandler
class in the java.net
package; that means that it's probably a bad idea to do
import java.net.*; import org.xml.sax.*;
In fact, "import ...*" is usually a sign of sloppy programming anyway, so the user should consider this a feature rather than a bug.
@since SAX 2.0 @author David Megginson @version 2.0.1+ (sax2r3pre1) @see org.xml.sax.XMLReader @see org.xml.sax.ErrorHandlerSAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the ContentHandler interface.
The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application's business rules). The information returned by the locator is probably not sufficient for use with a search engine.
Note that the locator will return correct information only during the invocation SAX event callbacks after {@link #startDocument startDocument} returns and before {@link #endDocument endDocument} is called. The application should not attempt to use it at any other time.
@param locator an object that can return the location of any SAX document event @see org.xml.sax.LocatorThe SAX parser will invoke this method only once, before any other event callbacks (except for {@link #setDocumentLocator setDocumentLocator}).
@throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exception @see #endDocumentThere is an apparent contradiction between the documentation for this method and the documentation for {@link org.xml.sax.ErrorHandler#fatalError}. Until this ambiguity is resolved in a future major release, clients should make no assumptions about whether endDocument() will or will not be invoked when the parser has reported a fatalError() or thrown an exception.
The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input.
@throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exception @see #startDocumentThe information from this event is not necessary for
normal Namespace processing: the SAX XML reader will
automatically replace prefixes for element and attribute
names when the http://xml.org/sax/features/namespaces
feature is true (the default).
There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary.
Note that start/endPrefixMapping events are not guaranteed to be properly nested relative to each other: all startPrefixMapping events will occur immediately before the corresponding {@link #startElement startElement} event, and all {@link #endPrefixMapping endPrefixMapping} events will occur immediately after the corresponding {@link #endElement endElement} event, but their order is not otherwise guaranteed.
There should never be start/endPrefixMapping events for the "xml" prefix, since it is predeclared and immutable.
@param prefix the Namespace prefix being declared. An empty string is used for the default element namespace, which has no prefix. @param uri the Namespace URI the prefix is mapped to @throws org.xml.sax.SAXException the client may throw an exception during processing @see #endPrefixMapping @see #startElementSee {@link #startPrefixMapping startPrefixMapping} for details. These events will always occur immediately after the corresponding {@link #endElement endElement} event, but the order of {@link #endPrefixMapping endPrefixMapping} events is not otherwise guaranteed.
@param prefix the prefix that was being mapped. This is the empty string when a default mapping scope ends. @throws org.xml.sax.SAXException the client may throw an exception during processing @see #startPrefixMapping @see #endElementThe Parser will invoke this method at the beginning of every element in the XML document; there will be a corresponding {@link #endElement endElement} event for every startElement event (even when the element is empty). All of the element's content will be reported, in order, before the corresponding endElement event.
This event allows up to three name components for each
element:
Any or all of these may be provided, depending on the values of the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes
properties:
Note that the attribute list provided will contain only
attributes with explicit values (specified or defaulted):
#IMPLIED attributes will be omitted. The attribute list
will contain attributes used for Namespace declarations
(xmlns* attributes) only if the
http://xml.org/sax/features/namespace-prefixes
property is true (it is false by default, and support for a
true value is optional).
Like {@link #characters characters()}, attribute values may have
characters that need more than one char
value.
The SAX parser will invoke this method at the end of every element in the XML document; there will be a corresponding {@link #startElement startElement} event for every endElement event (even when the element is empty).
For information on the names, see startElement.
@param uri the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed @param localName the local name (without prefix), or the empty string if Namespace processing is not being performed @param qName the qualified XML name (with prefix), or the empty string if qualified names are not available @throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exceptionThe Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.
The application must not attempt to read from the array outside of the specified range.
Individual characters may consist of more than one Java
char
value. There are two important cases where this
happens, because characters can't be represented in just sixteen bits.
In one case, characters are represented in a Surrogate Pair,
using two special Unicode values. Such characters are in the so-called
"Astral Planes", with a code point above U+FFFF. A second case involves
composite characters, such as a base character combining with one or
more accent characters.
Your code should not assume that algorithms using
char
-at-a-time idioms will be working in character
units; in some cases they will split characters. This is relevant
wherever XML permits arbitrary characters, such as attribute values,
processing instruction data, and comments as well as in data reported
from this method. It's also generally relevant whenever Java code
manipulates internationalized text; the issue isn't unique to XML.
Note that some parsers will report whitespace in element content using the {@link #ignorableWhitespace ignorableWhitespace} method rather than this one (validating parsers must do so).
@param ch the characters from the XML document @param start the start position in the array @param length the number of characters to read from the array @throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exception @see #ignorableWhitespace @see org.xml.sax.LocatorValidating Parsers must use this method to report each chunk of whitespace in element content (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.
SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.
The application must not attempt to read from the array outside of the specified range.
@param ch the characters from the XML document @param start the start position in the array @param length the number of characters to read from the array @throws org.xml.sax.SAXException any SAX exception, possibly wrapping another exception @see #charactersThe Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.
A SAX parser must never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.
Like {@link #characters characters()}, processing instruction
data may have characters that need more than one char
value.
The Parser will invoke this method each time the entity is
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities
and the
http://xml.org/sax/features/external-parameter-entities
properties.
If a SAX application needs to implement customized handling for external entities, it must implement this interface and register an instance with the SAX driver using the {@link org.xml.sax.XMLReader#setEntityResolver setEntityResolver} method.
The XML reader will then allow the application to intercept any external entities (including the external DTD subset and external parameter entities, if any) before including them.
Many SAX applications will not need to implement this interface, but it will be especially useful for applications that build XML documents from databases or other specialised input sources, or for applications that use URI types other than URLs.
The following resolver would provide the application with a special character stream for the entity with the system identifier "http://www.myhost.com/today":
import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; public class MyResolver implements EntityResolver { public InputSource resolveEntity (String publicId, String systemId) { if (systemId.equals("http://www.myhost.com/today")) { // return a special input source MyReader reader = new MyReader(); return new InputSource(reader); } else { // use the default behaviour return null; } } }
The application can also use this interface to redirect system identifiers to local URIs or to look up replacements in a catalog (possibly by using the public identifier).
@since SAX 1.0 @author David Megginson @version 2.0.1 (sax2r2) @see org.xml.sax.XMLReader#setEntityResolver @see org.xml.sax.InputSourceThe parser will call this method before opening any external entity except the top-level document entity. Such entities include the external DTD subset and external parameter entities referenced within the DTD (in either case, only if the parser reads external parameter entities), and external general entities referenced within the document element (if the parser reads external general entities). The application may request that the parser locate the entity itself, that it use an alternative URI, or that it use data provided by the application (as a character or byte input stream).
Application writers can use this method to redirect external system identifiers to secure and/or local URIs, to look up public identifiers in a catalogue, or to read an entity from a database or other input source (including, for example, a dialog box). Neither XML nor SAX specifies a preferred policy for using public or system IDs to resolve resources. However, SAX specifies how to interpret any InputSource returned by this method, and that if none is returned, then the system ID will be dereferenced as a URL.
If the system identifier is a URL, the SAX parser must resolve it fully before reporting it to the application.
@param publicId The public identifier of the external entity being referenced, or null if none was supplied. @param systemId The system identifier of the external entity being referenced. @return An InputSource object describing the new input source, or null to request that the parser open a regular URI connection to the system identifier. @exception org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception. @exception java.io.IOException A Java-specific IO exception, possibly the result of creating a new InputStream or Reader for the InputSource. @see org.xml.sax.InputSourceIf a SAX application needs to implement customized error handling, it must implement this interface and then register an instance with the XML reader using the {@link org.xml.sax.XMLReader#setErrorHandler setErrorHandler} method. The parser will then report all errors and warnings through this interface.
WARNING: If an application does not register an ErrorHandler, XML parsing errors will go unreported, except that SAXParseExceptions will be thrown for fatal errors. In order to detect validity errors, an ErrorHandler that does something with {@link #error error()} calls must be registered.
For XML processing errors, a SAX driver must use this interface in preference to throwing an exception: it is up to the application to decide whether to throw an exception for different types of errors and warnings. Note, however, that there is no requirement that the parser continue to report additional errors after a call to {@link #fatalError fatalError}. In other words, a SAX driver class may throw an exception after reporting any fatalError. Also parsers may throw appropriate exceptions for non-XML errors. For example, {@link XMLReader#parse XMLReader.parse()} would throw an IOException for errors accessing entities or the document.
@since SAX 1.0 @author David Megginson @version 2.0.1+ (sax2r3pre1) @see org.xml.sax.XMLReader#setErrorHandler @see org.xml.sax.SAXParseExceptionSAX parsers will use this method to report conditions that are not errors or fatal errors as defined by the XML recommendation. The default behaviour is to take no action.
The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end.
Filters may use this method to report other, non-XML warnings as well.
@param exception The warning information encapsulated in a SAX parse exception. @exception org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception. @see org.xml.sax.SAXParseExceptionThis corresponds to the definition of "error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a validating parser would use this callback to report the violation of a validity constraint. The default behaviour is to take no action.
The SAX parser must continue to provide normal parsing events after invoking this method: it should still be possible for the application to process the document through to the end. If the application cannot do so, then the parser should report a fatal error even if the XML recommendation does not require it to do so.
Filters may use this method to report other, non-XML errors as well.
@param exception The error information encapsulated in a SAX parse exception. @exception org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception. @see org.xml.sax.SAXParseExceptionThere is an apparent contradiction between the documentation for this method and the documentation for {@link org.xml.sax.ContentHandler#endDocument}. Until this ambiguity is resolved in a future major release, clients should make no assumptions about whether endDocument() will or will not be invoked when the parser has reported a fatalError() or thrown an exception.
This corresponds to the definition of "fatal error" in section 1.2 of the W3C XML 1.0 Recommendation. For example, a parser would use this callback to report the violation of a well-formedness constraint.
The application must assume that the document is unusable after the parser has invoked this method, and should continue (if at all) only for the sake of collecting additional error
messages:
If a SAX parser provides location information to the SAX application, it does so by implementing this interface and then passing an instance to the application using the content handler's {@link org.xml.sax.ContentHandler#setDocumentLocator setDocumentLocator} method. The application can use the object to obtain the location of any other SAX event in the XML source document.
Note that the results returned by the object will be valid only during the scope of each callback method: the application will receive unpredictable results if it attempts to use the locator at any other time, or after parsing completes.
SAX parsers are not required to supply a locator, but they are very strongly encouraged to do so. If the parser supplies a locator, it must do so before reporting any other document events. If no locator has been set by the time the application receives the {@link org.xml.sax.ContentHandler#startDocument startDocument} event, the application should assume that a locator is not available.
@since SAX 1.0 @author David Megginson @version 2.0.1 (sax2r2) @see org.xml.sax.ContentHandler#setDocumentLocatorThe return value is the public identifier of the document entity or of the external parsed entity in which the markup triggering the event appears.
@return A string containing the public identifier, or null if none is available. @see #getSystemIdThe return value is the system identifier of the document entity or of the external parsed entity in which the markup triggering the event appears.
If the system identifier is a URL, the parser must resolve it fully before passing it to the application. For example, a file name must always be provided as a file:... URL, and other kinds of relative URI are also resolved against their bases.
@return A string containing the system identifier, or null if none is available. @see #getPublicIdWarning: The return value from the method is intended only as an approximation for the sake of diagnostics; it is not intended to provide sufficient information to edit the character content of the original XML document. In some cases, these "line" numbers match what would be displayed as columns, and in others they may not match the source text due to internal entity expansion.
The return value is an approximation of the line number in the document entity or external parsed entity where the markup triggering the event appears.
If possible, the SAX driver should provide the line position of the first character after the text associated with the document event. The first line is line 1.
@return The line number, or -1 if none is available. @see #getColumnNumberchar
values since
the last line end.Warning: The return value from the method is intended only as an approximation for the sake of diagnostics; it is not intended to provide sufficient information to edit the character content of the original XML document. For example, when lines contain combining character sequences, wide characters, surrogate pairs, or bi-directional text, the value may not correspond to the column in a text editor's display.
The return value is an approximation of the column number in the document entity or external parsed entity where the markup triggering the event appears.
If possible, the SAX driver should provide the line position of the first character after the text associated with the document event. The first column in each line is column 1.
@return The column number, or -1 if none is available. @see #getLineNumberThis class can contain basic error or warning information from either the XML parser or the application: a parser writer or application writer can subclass it to provide additional functionality. SAX handlers may throw this exception or any exception subclassed from it.
If the application needs to pass through other types of exceptions, it must wrap those exceptions in a SAXException or an exception derived from a SAXException.
If the parser or application needs to include information about a specific location in an XML document, it should use the {@link org.xml.sax.SAXParseException SAXParseException} subclass.
@since SAX 1.0 @author David Megginson @version 2.0.1 (sax2r2) @see org.xml.sax.SAXParseExceptionThe existing exception will be embedded in the new one, and its message will become the default message for the SAXException.
@param e The exception to be wrapped in a SAXException.The existing exception will be embedded in the new one, but the new exception will have its own message.
@param message The detail message. @param e The exception to be wrapped in a SAXException.If there is an embedded exception, and if the SAXException has no detail message of its own, this method will return the detail message from the embedded exception.
@return The error or warning message.The feature name is any fully-qualified URI. It is possible for an XMLReader to recognize a feature name but temporarily be unable to return its value. Some feature values may be available only in specific contexts, such as before, during, or after a parse. Also, some feature values may not be programmatically accessible. (In the case of an adapter for SAX1 {@link Parser}, there is no implementation-independent way to expose whether the underlying parser is performing validation, expanding external entities, and so forth.)
All XMLReaders are required to recognize the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes feature names.
Typical usage is something like this:
XMLReader r = new MySAXDriver(); // try to activate validation try { r.setFeature("http://xml.org/sax/features/validation", true); } catch (SAXException e) { System.err.println("Cannot activate validation."); } // register event handlers r.setContentHandler(new MyContentHandler()); r.setErrorHandler(new MyErrorHandler()); // parse the first document try { r.parse("http://www.foo.com/mydoc.xml"); } catch (IOException e) { System.err.println("I/O exception reading XML document"); } catch (SAXException e) { System.err.println("XML exception reading document."); }
Implementors are free (and encouraged) to invent their own features, using names built on their own URIs.
@param name The feature name, which is a fully-qualified URI. @return The current value of the feature (true or false). @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the feature name but cannot determine its value at this time. @see #setFeatureThe feature name is any fully-qualified URI. It is possible for an XMLReader to expose a feature value but to be unable to change the current value. Some feature values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.
All XMLReaders are required to support setting http://xml.org/sax/features/namespaces to true and http://xml.org/sax/features/namespace-prefixes to false.
@param name The feature name, which is a fully-qualified URI. @param value The requested value of the feature (true or false). @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the feature name but cannot set the requested value. @see #getFeatureThe property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but temporarily be unable to return its value. Some property values may be available only in specific contexts, such as before, during, or after a parse.
XMLReaders are not required to recognize any specific property names, though an initial core set is documented for SAX2.
Implementors are free (and encouraged) to invent their own properties, using names built on their own URIs.
@param name The property name, which is a fully-qualified URI. @return The current value of the property. @exception org.xml.sax.SAXNotRecognizedException If the property value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the property name but cannot determine its value at this time. @see #setPropertyThe property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but to be unable to change the current value. Some property values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.
XMLReaders are not required to recognize setting any specific property names, though a core set is defined by SAX2.
This method is also the standard mechanism for setting extended handlers.
@param name The property name, which is a fully-qualified URI. @param value The requested value for the property. @exception org.xml.sax.SAXNotRecognizedException If the property value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the property name but cannot set the requested value.If the application does not register an entity resolver, the XMLReader will perform its own default resolution.
Applications may register a new or different resolver in the middle of a parse, and the SAX parser must begin using the new resolver immediately.
@param resolver The entity resolver. @see #getEntityResolverIf 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.
@param handler The content handler. @see #getContentHandlerIf 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.
@param handler The error handler. @see #getErrorHandlerThe return value is the public identifier of the document entity or of the external parsed entity in which the markup triggering the event appears.
@return A string containing the public identifier, or null if none is available. @see #getSystemIdThe return value is the system identifier of the document entity or of the external parsed entity in which the markup triggering the event appears.
If the system identifier is a URL, the parser must resolve it fully before passing it to the application. For example, a file name must always be provided as a file:... URL, and other kinds of relative URI are also resolved against their bases.
@return A string containing the system identifier, or null if none is available. @see #getPublicIdWarning: The return value from the method is intended only as an approximation for the sake of diagnostics; it is not intended to provide sufficient information to edit the character content of the original XML document. In some cases, these "line" numbers match what would be displayed as columns, and in others they may not match the source text due to internal entity expansion.
The return value is an approximation of the line number in the document entity or external parsed entity where the markup triggering the event appears.
If possible, the SAX driver should provide the line position of the first character after the text associated with the document event. The first line is line 1.
@return The line number, or -1 if none is available. @see #getColumnNumberchar
values since
the last line end.Warning: The return value from the method is intended only as an approximation for the sake of diagnostics; it is not intended to provide sufficient information to edit the character content of the original XML document. For example, when lines contain combining character sequences, wide characters, surrogate pairs, or bi-directional text, the value may not correspond to the column in a text editor's display.
The return value is an approximation of the column number in the document entity or external parsed entity where the markup triggering the event appears.
If possible, the SAX driver should provide the line position of the first character after the text associated with the document event. The first column in each line is column 1.
@return The column number, or -1 if none is available. @see #getLineNumberAn XML filter is like an XML reader, except that it obtains its events from another XML reader rather than a primary source like an XML document or database. Filters can modify a stream of events as they pass on to the final application.
The XMLFilterImpl helper class provides a convenient base for creating SAX2 filters, by passing on all {@link org.xml.sax.EntityResolver EntityResolver}, {@link org.xml.sax.DTDHandler DTDHandler}, {@link org.xml.sax.ContentHandler ContentHandler} and {@link org.xml.sax.ErrorHandler ErrorHandler} events automatically.
@since SAX 2.0 @author David Megginson @version 2.0.1 (sax2r2) @see org.xml.sax.helpers.XMLFilterImplThis method allows the application to link the filter to a parent reader (which may be another filter). The argument may not be null.
@param parent The parent reader.This method allows the application to query the parent reader (which may be another filter). It is generally a bad idea to perform any operations on the parent reader
directly:
This class is designed to sit between an {@link org.xml.sax.XMLReader XMLReader} and the client application's event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.
@since SAX 2.0 @author David Megginson @version 2.0.1 (sax2r2) @see org.xml.sax.XMLFilter @see org.xml.sax.XMLReader @see org.xml.sax.EntityResolver @see org.xml.sax.ContentHandler @see org.xml.sax.ErrorHandlerThis filter will have no parent: you must assign a parent before you start a parse or do any configuration with setFeature or setProperty, unless you use this as a pure event consumer rather than as an {@link XMLReader}.
@see org.xml.sax.XMLReader#setFeature @see org.xml.sax.XMLReader#setProperty @see #setParentThis is the {@link org.xml.sax.XMLReader XMLReader} from which this filter will obtain its events and to which it will pass its configuration requests. The parent may itself be another filter.
If there is no parent reader set, any attempt to parse or to set or get a feature or property will fail.
@param parent The parent XML reader. @see #getParentThis will always fail if the parent is null.
@param name The feature name. @param value The requested feature value. @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved from the parent. @exception org.xml.sax.SAXNotSupportedException When the parent recognizes the feature name but cannot set the requested value.This will always fail if the parent is null.
@param name The feature name. @return The current value of the feature. @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved from the parent. @exception org.xml.sax.SAXNotSupportedException When the parent recognizes the feature name but cannot determine its value at this time.This will always fail if the parent is null.
@param name The property name. @param value The requested property value. @exception org.xml.sax.SAXNotRecognizedException If the property value can't be assigned or retrieved from the parent. @exception org.xml.sax.SAXNotSupportedException When the parent recognizes the property name but cannot set the requested value.XMLReader is the interface that an XML parser's SAX2 driver must implement. This interface allows an application to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse.
All SAX interfaces are assumed to be synchronous: the {@link #parse parse} methods must not return until parsing is complete, and readers must wait for an event-handler callback to return before reporting the next event.
This interface replaces the (now deprecated) SAX 1.0 {@link org.xml.sax.Parser Parser} interface. The XMLReader interface contains two important enhancements over the old Parser interface (as well as some minor ones):
There are adapters available to convert a SAX1 Parser to a SAX2 XMLReader and vice-versa.
@since SAX 2.0 @author David Megginson @version 2.0.1+ (sax2r3pre1) @see org.xml.sax.XMLFilter @see org.xml.sax.helpers.ParserAdapter @see org.xml.sax.helpers.XMLReaderAdapterThe feature name is any fully-qualified URI. It is possible for an XMLReader to recognize a feature name but temporarily be unable to return its value. Some feature values may be available only in specific contexts, such as before, during, or after a parse. Also, some feature values may not be programmatically accessible. (In the case of an adapter for SAX1 {@link Parser}, there is no implementation-independent way to expose whether the underlying parser is performing validation, expanding external entities, and so forth.)
All XMLReaders are required to recognize the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes feature names.
Typical usage is something like this:
XMLReader r = new MySAXDriver(); // try to activate validation try { r.setFeature("http://xml.org/sax/features/validation", true); } catch (SAXException e) { System.err.println("Cannot activate validation."); } // register event handlers r.setContentHandler(new MyContentHandler()); r.setErrorHandler(new MyErrorHandler()); // parse the first document try { r.parse("http://www.foo.com/mydoc.xml"); } catch (IOException e) { System.err.println("I/O exception reading XML document"); } catch (SAXException e) { System.err.println("XML exception reading document."); }
Implementors are free (and encouraged) to invent their own features, using names built on their own URIs.
@param name The feature name, which is a fully-qualified URI. @return The current value of the feature (true or false). @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the feature name but cannot determine its value at this time. @see #setFeatureThe feature name is any fully-qualified URI. It is possible for an XMLReader to expose a feature value but to be unable to change the current value. Some feature values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.
All XMLReaders are required to support setting http://xml.org/sax/features/namespaces to true and http://xml.org/sax/features/namespace-prefixes to false.
@param name The feature name, which is a fully-qualified URI. @param value The requested value of the feature (true or false). @exception org.xml.sax.SAXNotRecognizedException If the feature value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the feature name but cannot set the requested value. @see #getFeatureThe property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but temporarily be unable to return its value. Some property values may be available only in specific contexts, such as before, during, or after a parse.
XMLReaders are not required to recognize any specific property names, though an initial core set is documented for SAX2.
Implementors are free (and encouraged) to invent their own properties, using names built on their own URIs.
@param name The property name, which is a fully-qualified URI. @return The current value of the property. @exception org.xml.sax.SAXNotRecognizedException If the property value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the property name but cannot determine its value at this time. @see #setPropertyThe property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but to be unable to change the current value. Some property values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.
XMLReaders are not required to recognize setting any specific property names, though a core set is defined by SAX2.
This method is also the standard mechanism for setting extended handlers.
@param name The property name, which is a fully-qualified URI. @param value The requested value for the property. @exception org.xml.sax.SAXNotRecognizedException If the property value can't be assigned or retrieved. @exception org.xml.sax.SAXNotSupportedException When the XMLReader recognizes the property name but cannot set the requested value.If the application does not register an entity resolver, the XMLReader will perform its own default resolution.
Applications may register a new or different resolver in the middle of a parse, and the SAX parser must begin using the new resolver immediately.
@param resolver The entity resolver. @see #getEntityResolverIf 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.
@param handler The content handler. @see #getContentHandlerIf 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.
@param handler The error handler. @see #getErrorHandler