Java Reference
In-Depth Information
methodthrows
SAXNotRecognizedException
whenthenameisnotre-
cognized as a property, and throws
SAXNotSupportedException
when
thenameisrecognizedbuttheassociatedvaluecannotbesetwhen
setProp-
erty()
is called.
Ifahandlerisnotinstalled,alleventspertainingtothathandleraresilentlyignored.
Notinstallinganerrorhandlercanbeproblematicbecausenormalprocessingmightnot
continue, and the application would not be aware that anything had gone wrong. If an
entityresolverisnotinstalled, theparserperformsitsowndefaultresolution.I'llhave
more to say about entity resolution later in this chapter.
Note
Youcan install anewcontent handler,DTDhandler,entity resolver,orerror
handlerwhilethedocumentisbeingparsed.Theparserstartsusingthehandlerwhen
the next event occurs.
Afterobtainingan
XMLReader
instance,youcanconfigurethatinstancebysetting
itsfeaturesandproperties.A
feature
isaname-valuepairthatdescribesaparsermode,
suchasvalidation.Incontrast,a
property
isaname-valuepairthatdescribessomeother
aspectoftheparserinterface,suchasalexicalhandlerthataugmentsthecontenthand-
lerbyprovidingcallbackmethodsforreportingoncomments,CDATAdelimiters,anda
few other syntactic constructs.
Featuresandpropertieshavenames,whichmustbeabsoluteURIsbeginningwiththe
http://
prefix.Afeature'svalueisalwaysaBooleantrue/falsevalue.Incontrast,a
property's value is an arbitrary object. The following example demonstrates setting a
feature and a property:
xmlr.setFeature("http://xml.org/sax/features/validation",
true);
xmlr.setProperty("http://xml.org/sax/properties/lexical-
handler",
new LexicalHandler() { /* … */ });
The
setFeature()
callenablesthe
validation
featuresothattheparserwill
performvalidation.Featurenamesareprefixedwith
http://xml.org/sax/fea-
Note
Parsers must support the
namespaces
and
namespace-prefixes
fea-
tures.
namespaces
decides whether URIs and local names are passed to
Con-
tentHandler
's
startElement()
and
endElement()
methods. It defaults to
true
—these names are passed. The parser can pass empty strings when
false
.