↑
Main Page
NodeFilter
To create a
NodeIterator
object, use the
createNodeIterator()
method of the
document
object. This
method accepts four arguments:
1.
root
— the node in the tree that you wish to start searching from
2.
whatToShow
— a numerical code indicating which nodes should be visited
3.
filter
—a
NodeFilter
object to determine which nodes to ignore
4.
entityReferenceExpansion
— a Boolean value indicating whether entity references should
be expanded
The
whatToShow
argument determines which nodes to visit by applying one or more of the following
constants:
?
NodeFilter.SHOW_ALL
— show all node types
?
NodeFilter.SHOW_ELEMENT
— show element nodes
?
NodeFilter.SHOW_ATTRIBUTE
— show attribute nodes
?
NodeFilter.SHOW_TEXT
— show text nodes
?
NodeFilter.SHOW_CDATA_SECTION
— show CData section nodes
?
NodeFilter.SHOW_ENTITY_REFERENCE
— show entity reference nodes
?
NodeFilter.SHOW_ENTITY
— show entity nodes
?
NodeFilter.SHOW_PROCESSING_INSTRUCTION
— show PI nodes
?
NodeFilter.SHOW_COMMENT
— show comment nodes
?
NodeFilter.SHOW_DOCUMENT
— show document nodes
?
NodeFilter.SHOW_DOCUMENT_TYPE
— show document type nodes
?
NodeFilter.SHOW_DOCUMENT_FRAGMENT
— show document fragment nodes
?
NodeFilter.SHOW_NOTATION
— show notation nodes
You can combine multiple values by using the bitwise OR operator:
var iWhatToShow = NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT;
The filter argument of
createNodeIterator()
can be used to specify a custom
NodeFilter
object, but
can also be left
null
if you don’t want to use it.
To create a simple
NodeIterator
that visits all node types, use the following:
var iterator = document.createNodeIterator(document, NodeFilter.SHOW_ALL, null,
false);
To move forward and backward in the search, use the
nextNode()
and
previousNode()
methods:
var node1 = iterator.nextNode();
var node2 = iterator.nextNode();
184
Chapter 6
09_579088 ch06.qxd 3/28/05 11:37 AM Page 184
Free JavaScript Editor
Ajax Editor
©
→