Java Reference
In-Depth Information
stract
FactoryConfigurationError
and
ParserConfigurationExcep-
tion
classes. The
org.w3c.dom
,
org.w3c.dom.bootstrap
,
org.w3c.dom.events
, and
org.w3c.dom.ls
packages provide various types
that augment this implementation.
ThefirststepinworkingwithDOMistoinstantiate
DocumentBuilderFactory
by calling one its
newInstance()
methods. For example, the following example
invokes
DocumentBuilderFactory
's
static DocumentBuilderFactory
newInstance()
method:
DocumentBuilderFactory
dbf
=
DocumentBuilderFact-
ory.newInstance();
Behindthescenes,
newInstance()
followsanorderedlookupproceduretoidenti-
fy the
DocumentBuilderFactory
implementation class to load. This procedure
first examines the
javax.xml.parsers.DocumentBuilderFactory
system
property, and lastly chooses the Java platform's default
DocumentBuilderFact-
ory
implementation class when no other class is found. If an implementation class
is not available (perhaps the class identified by the
javax.xml.parsers.DocumentBuilderFactory
system property doesn't
exist) or cannot be instantiated,
newInstance()
throws an instance of the
Fact-
oryConfigurationError
class.Otherwise,itinstantiatestheclassandreturnsits
instance.
After obtaining a
DocumentBuilderFactory
instance, you can call various
configuration methods to configure the factory. For example, you could call
Docu-
mentBuilderFactory
's
void setNamespaceAware(boolean aware-
ness)
method with a
true
argument to tell the factory that any returned parser
(known as a
document builder
to DOM) must provide support for XML namespaces.
Youcanalsocall
void setValidating(boolean validating)
with
true
as the argument to validate documents against their DTDs, or call
void setS-
chema(Schema schema)
to validate documents against the
javax.xml.validation.Schema
instance identified by
schema
.
VALIDATION API
JAXP includes the Validation API to decouple document parsing from validation,
which makes it easier for applications to take advantage of specialized validation
libraries that support additional schema languages (e.g., Relax NG—see
ht-