You are here: Reference > JavaScript > client-side > HTML DOM > methods > createNodeIterator (document, XMLDocument)

createNodeIterator method (document, XMLDocument)

Browser support:
93.5
Creates a NodeIterator object that can be used to iterate through the nodes in a subtree.
Note: The createNodeIterator method is supported and implemented in Firefox from version 3.5. In the earlier versions of Firefox, it is supported but not implemented, using it raises an exception.

The NodeIterator object represents the result of the createNodeIterator method as an ordered list. The members of the NodeIterator object can be used to navigate through the result list. For a detailed description, please see the page for the NodeIterator object.

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:

object.createNodeIterator (rootNode, nodeType, filterFunction, entityRefExpansion);
You can find the related objects in the Supported by objects section below.

Parameters:

rootNode
Reference to a node that will be the root node of the NodeIterator object.
nodeType
Integer that specifies the type of nodes to show.
Predefined constants are available for the possible values of this parameter, in the scope of the NodeFilter interface (e.g. NodeFilter.SHOW_ALL). The value can be any combination of the following integer constants with the bitwise OR operator (the value of a predefined constant appears in parentheses after the constant in hexadecimal form):
SHOW_ALL (0xFFFFFFFF)
Show all nodes.
SHOW_ATTRIBUTE (0x00000002)
Show attribute nodes.
SHOW_CDATA_SECTION (0x00000008)
Show CDATASection nodes.
SHOW_COMMENT (0x00000080)
Show comment nodes.
SHOW_DOCUMENT (0x00000100)
Show document nodes.
SHOW_DOCUMENT_FRAGMENT (0x00000400)
Show DocumentFragment nodes.
SHOW_DOCUMENT_TYPE (0x00000200)
Show DocumentType nodes.
SHOW_ELEMENT (0x00000001)
Show element nodes.
SHOW_ENTITY (0x00000020)
Show Entity nodes.
SHOW_ENTITY_REFERENCE (0x00000010)
Show EntityReference nodes.
SHOW_NOTATION (0x00000800)
Show Notation nodes.
SHOW_PROCESSING_INSTRUCTION (0x00000040)
Show ProcessingInstruction nodes.
SHOW_TEXT (0x00000004)
Show text nodes.
filterFunction
Reference to a user-specified function that can filter nodes.
The method gets a node for checking and must return an integer value as a result. Predefined constants are available for the possible results, in the scope of the NodeFilter interface (e.g. NodeFilter.FILTER_ACCEPT). The result can be one of the following values (the value of a predefined constant appears in parentheses after the constant):
  • FILTER_ACCEPT (1): Accept the node.
  • FILTER_REJECT (2): Reject the node. Equivalent to the FILTER_SKIP value.
  • FILTER_SKIP (3): Skip the node.
entityRefExpansion
Boolean that indicates whether the contents of EntityReference nodes are visible for the NodeIterator object.
One of the following values:
false
The contents of EntityReference nodes are hidden.
true
The contents of EntityReference nodes are visible.

Return value:

Returns the newly created NodeIterator object.

Example HTML code 1:

This example illustrates the use of the createNodeIterator method:
<head>
    <script type="text/javascript">
        function ElementChecker (node) {
            if (node.tagName.toLowerCase () == 'button') {
                return NodeFilter.FILTER_ACCEPT;
            }
            return NodeFilter.FILTER_SKIP;
        }

        function FindFirstButton () {
                // try..catch is necessary because the createNodeIterator method is supported but not implemented in Firefox before version 3.5.
            try {
                iterator = document.createNodeIterator (document, NodeFilter.SHOW_ELEMENT, ElementChecker, false);

                    // get the first matching node
                var button = iterator.nextNode ();
                
                alert ("The label of the first button element:\n" + button.textContent);
            }
            catch (e) {
                alert ("Your browser does not support the createNodeIterator method!");
            }
        }
    </script>
</head>
<body>
    <button onclick="FindFirstButton ()">Find the first button tag with the NodeIterator object</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

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

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content