Advanced SAX - Setting Properties and Features
(Page 2 of 4 )
In SAX, a property is a setting that requires passing in some Object argument for the parser to use; for instance, certain types of handlers are set by specifying a URI and supplying the Object that implements that handler’s interface. A feature is a setting that is either on ( true ) or off ( false ). Several obvious examples come to mind: namespace awareness and validation, for example.
SAX includes the methods needed for setting properties and features in the XMLReader interface. This means you have to change very little of your existing code to request validation, set the namespace separator, and handle other feature and property requests. The methods used for setting these properties and features are outlined in Table 4-1.
Table 4-1. You’ll use features almost every time you write a program to parse XML; properties are less commonly used, but still important in your XML programming.
Method | Returns | Parameters | Syntax |
setProperty() | void | String propertyID, Object value | parser.setProperty("[Property URI]", propertyValue); |
setFeature() | void | String featureID, boolean state | parser.setFeature("[Feature URI]", featureState); |
getProperty() | Object | String propertyID | Object propertyValue = parser. getProperty("[Property URI]"); |
getFeature() | boolean | String featureID | boolean featureState = parser. getFeature("[Feature URI]"); |
For all of these, the ID of a specific property or feature is a URI. The standard set of features and properties is listed in the Appendix 0. Additionally, most parsers define additional, vendor-specific properties that their parser supports. For example, Apache Xerces defines quite a few additional features and properties, listed online at http://xml.apache.org/xerces2-j/features.html and http://xml.apache.org/xerces2-j/ properties.html.
The most convenient aspect of these methods is that they allow simple addition and modification of properties and features. Although new or updated features will require a parser implementation to add supporting code, the method by which features and
My Feature URI Is a 404!
Feature and property URIs are similar to namespace URIs; they are only used as identifiers for particular options. If you typed one of these URIs into a browser, you’d probably get a nice 404 Not Found error.
Good parsers ensure that you do not need network access to resolve these features and properties; think of them as simple constants that happen to be in URI form. These methods are simply invoked and the URI is dereferenced locally, often to constantly represent what action in the parser needs to be taken.
properties are accessed remains standard and simple; only a new URI need be defined. Regardless of the complexity (or obscurity) of new XML-related ideas, this robust set of four methods should be sufficient to allow parsers to implement the new ideas.
Next: Error Handling >>
More Java Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Java and XML, Third Edition, written by Brett McLaughlin and Justin Edelson (O'Reilly, 2006; ISBN: 059610149X). Check it out today at your favorite bookstore. Buy this book now.
|
|