Traversal API
Object: NodeFilter
The NodeFilter object is used to filter out nodes. If a NodeIterator or TreeWalker is given a NodeFilter, it applies the filter before it returns the next node. If the filter says to accept the node, the traversal object, either a NodeIterator or TreeWalker, returns it; otherwise, the traversal object looks for the next node and acts as if that the node that was rejected was not there.
NodeFilter constants
Constants returned by NodeFilter.acceptNode().
|
Constant |
Value type |
Description |
Support level |
Availability |
|---|---|---|---|---|
|
1 |
FILTER_ACCEPT |
Accepts the node. This node will be returned with the results of a NodeIterator or TreeWalker object. |
DOM 2 Traversal |
4.6 or later |
|
2 |
FILTER_REJECT |
Rejects the node. This node will not be returned with the results of a NodeIterator or TreeWalker object. For TreeWalker objects, the children of this node are also excluded. |
DOM 2 Traversal |
4.6 or later |
|
3 |
FILTER_SKIP |
Skips the node. This node will not be returned with the results of a NodeIterator or TreeWalker object. Children of this node are still considered. |
DOM 2 Traversal |
4.6 or later |
Constants for the NodeIterator.whatToShow and TreeWalker.whatToShow properties.
|
Constant |
Value type |
Description |
Support level |
Availability |
|---|---|---|---|---|
|
0xFFFFFFFF |
SHOW_ALL |
Shows all nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000001 |
SHOW_ELEMENT |
Shows Element nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000002 |
SHOW_ATTRIBUTE |
Shows Attr nodes. Because attributes are not the child of any other node, they never appear in the NodeIterator’s or TreeWalkers's logical view unless they are defined as the root of the object. If the respective node is not defined as the root, then SHOW_ATTRIBUTE has no meaning. |
DOM 2 Traversal |
4.6 or later |
|
0x00000004 |
SHOW_TEXT |
Shows Text nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000008 |
SHOW_CDATA_SECTION |
Shows CDATASection nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000010 |
SHOW_ENTITY_REFERENCE |
Shows EntityReference nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000020 |
SHOW _ENTITY |
Shows Entity nodes. Because entities are not the child of any other node, they never appear in the NodeIterator’s or TreeWalkers's logical view unless they are defined as the root of the object. If the respective node is not defined as the root, then SHOW_ENTITY has no meaning. |
DOM 2 Traversal |
4.6 or later |
|
0x00000040 |
SHOW_PROCESSING_INSTRUCTION |
Shows ProcessingInstruction nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000080 |
SHOW_COMMENT |
Shows Comment nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000100 |
SHOW_DOCUMENT |
Shows Document nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000200 |
SHOW_DOCUMENT_TYPE |
Shows DocumentType nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000400 |
SHOW_DOCUMENT_FRAGMENT |
Shows DocumentFragment nodes. |
DOM 2 Traversal |
4.6 or later |
|
0x00000800 |
SHOW_NOTATION |
Shows Notation nodes. Because notations are not the child of any other node, they never appear in the NodeIterator’s or TreeWalkers's logical view unless they are defined as the root of the object. If the respective node is not defined as the root, then SHOW_NOTATION has no meaning. |
DOM 2 Traversal |
4.6 or later |
NodeFilter methods
|
Method name |
Description |
Support level |
Availability |
|---|---|---|---|
|
acceptNode() |
Tests whether a specified node is visible in the logical view of a NodeIterator or TreeWalker. Returns a NodeFilter constant. |
4.6 or later |
Object: NodeIterator
The NodeIterator object enables you to navigate up and down through a flat representation of the document tree. This object is similar to the TreeWalker objects, except that TreeWalker presents nodes in a tree-oriented view, rather than the list view presented by NodeIterator.
Create a NodeIterator using Document.createNodeIterator().
NodeIterator properties
|
Property name |
Type |
Description |
Status |
Support level |
Availability |
|---|---|---|---|---|---|
|
expandEntityReferences |
boolean |
Returns whether or not the NodeIterator expands the entity references it encounters as it traverses the document. |
read only |
4.6 or later |
|
|
filter |
Returns the NodeFilter used to filter nodes for traversal. |
read only |
4.6 or later |
||
|
root |
Returns the Node at which the NodeIterator begins traversing the document. |
read only |
4.6 or later |
||
|
whatToShow |
unsigned long |
Returns which node types are presented by the NodeIterator, represented by one or more NodeFilter constants. Multiple values may be specified using a bitwise OR. |
read only |
4.6 or later |
NodeIterator methods
|
Method name |
Description |
Support level |
Availability |
|---|---|---|---|
|
detach() |
Detaches the NodeIterator from the set which it iterated over. |
4.6 or later |
|
|
nextNode() |
Moves the NodeIterator to the next visible Node in the document order, relative to the current node. |
4.6 or later |
|
|
previousNode() |
Moves the NodeIterator to the previous visible Node in the document order, relative to the current node. |
4.6 or later |
Object: TreeWalker
The TreeWalker object enables you to navigate the document tree or subtree. This object is similar to the NodeIterator objects, except that TreeWalker presents nodes in a tree-oriented view, rather than the list view presented by NodeIterator. This allows you to use TreeWalker to navigate directly to a parent or child, rather than simply up and down in the list using NodeIterator.
Create a TreeWalker using Document.createTreeWalker().
TreeWalker properties
|
Property name |
Type |
Description |
Status |
Support level |
Availability |
|---|---|---|---|---|---|
|
currentNode |
Specifies the current position of the TreeWalker. |
writable |
4.6 or later |
||
|
expandEntityReferences |
boolean |
Returns whether or not the TreeWalker expands the entity references it encounters as it traverses the document. |
read only |
4.6 or later |
|
|
filter |
Returns the NodeFilter used to filter nodes for traversal. |
read only |
4.6 or later |
||
|
root |
Returns the Node at which the TreeWalker begins traversing the document. |
read only |
4.6 or later |
||
|
whatToShow |
unsigned long |
Returns which node types are presented by the TreeWalker, represented by one or more NodeFilter constants. Multiple values may be specified using a bitwise OR. |
read only |
4.6 or later |
TreeWalker methods
|
Method name |
Description |
Support level |
Availability |
|---|---|---|---|
|
firstChild() |
Moves the TreeWalker to the first visible child of the current node. |
4.6 or later |
|
|
lastChild() |
Moves the TreeWalker to the last visible child of the current node. |
4.6 or later |
|
|
nextNode() |
Moves the TreeWalker to the next visible node in the document order, relative to the current node. |
4.6 or later |
|
|
nextSibling() |
Moves the TreeWalker to the next sibling of the current node. |
4.6 or later |
|
|
parentNode() |
Moves the TreeWalker to the closest visible ancestor node of the current node. |
4.6 or later |
|
|
previousNode() |
Moves the TreeWalker to the previous visible node in the document order, relative to the current node. |
4.6 or later |
|
|
previousSibling() |
Moves the TreeWalker to the previous sibling of the current node. |
4.6 or later |