You are here: Reference > JavaScript > client-side > xml handling > objects > NodeIterator

NodeIterator object

Browser support:
93.5
Represents the result of the createNodeIterator method as an ordered list.
Note: The NodeIterator object is supported in Internet Explorer from version 9.
The elements of the ordered list refer to the nodes in the document that fulfill the conditions specified for the createNodeIterator method. An element (element1) precedes another element (element2) in the ordered list if and only if the node referred to by the element1 precedes the node referred to by the element2 in the document tree.
The root, whatToShow, filter and expandEntityReferences properties of the NodeIterator object retrieve the settings that were specified earlier for the createNodeIterator method to create the NodeIterator object. The other members can be used to iterate through the result list.
Note that the createNodeIterator method is supported but not implemented in Firefox before version 3.5, using it raises an exception.
The createTreeWalker method and the TreeWalker object are similar to the createNodeIterator method and the NodeIterator object. The main difference is that the TreeWalker object represents the result as a tree not as an ordered list.

Syntax:

Methods that return the object:
object.createNodeIterator (rootNode, nodeType, filterFunction, entityRefExpansion)
Related objects:

Possible members:

Properties:
expandEntityReferences
Returns a Boolean value that indicates whether the children of entity reference nodes are visible to the NodeIterator or TreeWalker object.
filter
Returns the filtering that function belongs to the NodeIterator or TreeWalker object.
root
Returns the root element that was used to create the NodeIterator or TreeWalker object.
whatToShow
Returns an integer that identifies the types of nodes that can be shown by the current NodeIterator or TreeWalker object.
Methods:
detach
Releases the current NodeIterator object and puts the iterator into the invalid state.
nextNode
Finds the next node relative to the current node in the ordered list represented by the current NodeIterator object or in the tree represented by the current TreeWalker object. If it exists, it will be made the current node and returned, else the current node is retained and null is returned.
previousNode
Finds the previous node relative to the current node in the ordered list represented by the current NodeIterator object or in the tree represented by the current TreeWalker object. If it exists, it will be made the current node and returned, else the current node is retained and null is returned.

Example HTML code 1:

This example shows how to iterate through span elements with the NodeIterator object:
<head>
    <script type="text/javascript">
        function ElementChecker (node) {
            if (node.tagName.toLowerCase () == 'span') {
                return NodeFilter.FILTER_ACCEPT;
            }
            return NodeFilter.FILTER_SKIP;
        }

        function FindMainSections () {
            var contElem = document.getElementById ("content");
                // try..catch is necessary because the createNodeIterator method is supported but not implemented in Firefox before version 3.5.
            try {
                iterator = document.createNodeIterator (contElem, NodeFilter.SHOW_ELEMENT, ElementChecker, false);
                
                    // get the first matching node
                var node = iterator.nextNode ();

                while (node) {
                    alert ("The contents of the section:\n " + node.innerHTML);
                    node = iterator.nextNode ();
                }
            }
            catch (e) {
                alert ("Your browser does not support the createNodeIterator method!");
            }
        }
    </script>
</head>
<body>
    <div id="content">
        <span>
            <b>1. Section</b><br />
            <span>
                <b>1.1. Subsection</b><br />
            </span>
        </span>
        <span>
            <b>2.Section</b><br />
        </span>
    </div>

    <br /><br />
    <button onclick="FindMainSections ()">Find the sections with the NodeIterator object</button>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content