Java Reference
In-Depth Information
Note
Apart from
DocumentBuilder
,
DocumentBuilderFactory
, and a
few other classes, DOM is based on interfaces, of which
Document
and
Docu-
mentType
are examples. Behind the scenes, DOM methods (such as the
parse()
methods) return objects whose classes implement these interfaces.
Document
andall other
org.w3c.dom
interfaces that describe different kindsof
nodes are subinterfaces of the
org.w3.dom.Node
interface. As such, they inherit
Node
's constants and methods.
Node
declares twelve constants that represent the various kinds of nodes;
ATTRIBUTE_NODE
and
ELEMENT_NODE
are examples. When you want to identify
the kind of node represented by a given
Node
object, call
Node
's
short
getNodeType()
method and compare the returned value to one of these constants.
Note
Therationaleforusing
getNodeType()
andtheseconstants,insteadofus-
ing
instanceof
andaclassname,isthatDOM(theobjectmodel,nottheJavaDOM
API) was designed to be language independent, and languages such as AppleScript
don't have the equivalent of
instanceof
.
Node
declares several methods for getting and setting common node properties.
Thesemethodsinclude
String getNodeName()
,
String getLocalName()
,
String getNamespaceURI()
,
String getPrefix()
,
void setPre-
fix(String prefix)
,
String getNodeValue()
, and
void
setNodeValue(String nodeValue)
, which let you get and (for some proper-
ties)setanode'sname(suchas
#text
),localname,namespaceURI,prefix,andnor-
malized string value properties.
Note
Various
Node
methods (e.g.,
setPrefix()
and
getNodeValue()
)
throw an instance of the
org.w3c.dom.DOMException
class when something
goes wrong. For example,
setPrefix()
throws this exception when the
prefix
argument contains an illegal character, the node is read-only, or the argument is
malformed. Similarly,
getNodeValue()
throws
DOMException
when
getNodeValue()
would return more characters than can fit into a
DOMString
(a W3C type) variable on the implementation platform.
DOMException
declares a
seriesofconstants(suchas
DOMSTRING_SIZE_ERR
)thatclassifythereasonforthe
exception.
Node
declaresseveralmethodsfornavigatingthenodetree.Threeofitsnavigation
methods are
boolean hasChildNodes()
(return true when a node has child