Java Reference
In-Depth Information
builderFactory.setNamespaceAware(true); // Set namespace aware
builderFactory.setValidating(true); // and validating parser features
builderFactory.setIgnoringElementContentWhitespace(true);
Calling this method will result in a parser that will not report ignorable whitespace as a node, so you
won't see it in the
Document
object. If you run the example again with this change, the
#text
nodes
arising from ignorable whitespace will no longer be there. Of course, this is not necessarily a plus since
now the output produced by the
toString()
method is not as readable as it was before because
everything appears on a single line.
Node Types
We saw earlier that the subinterfaces of
Node
identify nodes of different types. Type
Element
corresponds to an element and type
Text
identifies element content that is text. We also saw how we
could determine the type of a given
Node
reference using the
instanceof
operator. There's another
way of figuring out what a
Node
reference is that is often more convenient. The
getNodeType()
method in the
Node
interface returns a value of type
int
that identifies the type of node. It can be any
of the following constant values that are defined in the
Node
interface:
DOCUMENT
_
NODE
DOCUMENT
_
TYPE
_
NODE
ELEMENT
_
NODE
ATTRIBUTE
_
NODE
TEXT
_
NODE
CDATA
_
SECTION
_
NODE
DOCUMENT
_
FRAGMENT
_
NODE
COMMENT
_
NODE
ENTITY
_
NODE
ENTITY
_
REFERENCE
_
NODE
NOTATION
_
NODE
PROCESSING
_
INSTRUCTION
_
NODE
In the main it is obvious what type each of these represents. The
DOCUMENT
_
FRAGMENT
_
NODE
represents a
collection of elements that form part of a document. The advantage of having the type of node as an integer
is that we can sort out what type a given
Node
reference is by using a
switch
statement:
switch(node.getNodeType()) {
case Node.DOCUMENT
_
NODE:
// Code to process a document node
break;
case Node.DOCUMENT
_
TYPE
_
NODE:
// Code to process a DOCTYPE node
break;
case Node.DOCUMENT
_
NODE:
// Code to process a document node
break;
case Node.ELEMENT
_
NODE:
// Code to process an element node
break;
// ... and so on for the rest of the type values...
default:
assert false;
}