XML technologies / XML / XML in a Nutshell / The Simple API for XML (SAX) / Features and properties

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

SAX uses properties and features to control the parser. Each is named by an absolute URI. Like a namespace URI, a feature or property URI has no technical content; in particular, do not expect useful output from entering it in a browser. Its sole purpose is to provide a unique name. Features are either true or false, that is, Boolean values. A property, however, can have any data type. In Java properties therefore have the type Object. Different parsers support different sets of features and properties, although most support a few common ones by default.

The feature "http://xml.org/sax/features/validation" controls whether a parser validates. If its value is true, the parser checks document validity. Invalidity is reported through an ErrorHandler if one is registered; otherwise validity problems are ignored. The default is false. To enable a feature, call the XMLReader method setFeature( ) with the feature name and the value true:

try {
  parser.setFeature("http://xml.org/sax/features/validation", true);
}
catch (SAXNotSupportedException ex) {
  System.out.println("Die Validierung kann momentan nicht eingeschaltet werden.");
}
catch (SAXNotRecognizedException ex) {
  System.out.println("Dies ist kein validierender Parser.");
}

Not all parsers can validate. Trying to enable validation in an incapable parser, or to set any unrecognized feature, causes the corresponding call to setFeature( ) to raise a SAXNotRecognizedException. If a feature is recognized but cannot currently be changed, the method setFeature( ) raises a SAXNotSupported-Exception. This can happen, for example, when enabling validation after the parser has already read half the document. Both exceptions are subclasses of SAXException.

The current feature value can be checked with the XMLReader method getFeature( ). Its return value has the type boolean, but calling it can raise the same exceptions as setFeature( ). To find out whether a parser validates, use:

try {
  boolean isValidating =
   parser.getFeature("http://xml.org/sax/features/validation");
}
catch (SAXException e) {
  System.out.println("Dies ist kein validierender Parser.");
}

A property resembles a feature but can take values other than true or false. A property’s value is an object of an unspecified type. To determine the document’s XML version, 1.0 or 1.1, use the method getProperty( ) to read, for example, the property "http://xml.org/sax/properties/document-xml-version":

try {
  String version = (String) parser.getProperty(
   "http://xml.org/sax/properties/document-xml-version");
  if ("1.0".equals(version) {
    System.out.println("Ein gutes konservatives Dokument");
  }
  else if ("1.1".equals(version) {
    System.out.println("Ein gefährlich radikales Dokument");
  }
  else {
    System.out.println("Ein sehr seltsames Dokument: " + version);
  }
}
catch (SAXNotSupportedException ex) {
  System.out.println(
    "Kann die vom Dokument verwendete XML-Version zurzeit nicht angeben.");
}
catch (SAXNotRecognizedException ex) {
  System.out.println("Der Parser erkennt das " +
   "http://xml.org/sax/properties/document-xml-version-Property nicht");
}

A property value can be changed by calling the method setProperty( ) with two arguments: the property URI and the object containing its new value. The following example tries to set the property "http://xml.org/sax/properties/LexicalHandler" to an instance of the class MyLexicalHandlerClass. The parser reports lexical events (comments, CDATA sections, and entity references) through this object, which must implement the class org.xml.sax.ext.LexicalHandler:

try {
  parser.setProperty(
    "http://xml.org/sax/properties/LexicalHandler",
    new MyLexicalHandlerClass(  )
  );
}
catch (SAXException ex) {
  System.out.println("Dieser Parser kennt keine lexikalischen Events.");
}

Passing the wrong type of object to a property causes a call to the method setProperty( ) raises a SAXNotSupportedException. For example, this happens when an object not implementing the interface LexicalHandler is passed to the property "http://xml.org/sax/properties/LexicalHandler".

Not all features and properties can be set. Some, such as "http://xml.org/sax/properties/document-xml-version" are read-only. Others can be set only at certain times. For example, validation cannot be enabled after half the document has been read; attempting to do so raises a SAXNotSupportedException. The same feature can, however, be set between documents, after reading one and before reading another. Most features and properties can be read at any time, although some, such as "http://xml.org/sax/properties/document-xml-version", can be read only after the parser has started reading the document.

  

 

 

 

back to top