You are here: Reference > JavaScript > client-side > xml handling > methods > iterateNext (XPathResult)

iterateNext method (XPathResult)

Browser support:
Moves the iterator to the next node in the result set and returns the node referenced by the current iterator.
The XPathResult object represents the result of the evaluate method. The type of the result depends on the fourth parameter of the evaluate method, or if its value is ANY_TYPE, then it depends on the expression specified by the first parameter.
With the resultType property the type of the result can be retrieved. If the value of the resultType property is UNORDERED_NODE_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE, then the result contains all nodes that match the expression. In that case, the iterateNext method can be used to retrieve the matching nodes from the result set.
The iterateNext method is similar to the snapshotItem method. Both provide access to the matching nodes. The main difference is that modifying the document invalidates the iteration, but does not invalidate the snapshots collection.

Syntax:

object.iterateNext ( );
You can find the related objects in the Supported by objects section below.

Return value:

Returns the next node from the result set or null if there are no more matching nodes.

Example HTML code 1:

This example shows how to get all span elements that are immediate children of a given element:
<head>
    <style>
        .level1 {color:red;}
        .level2 {color:blue;}
        .level3 {color:green;}
    </style>
    <script type="text/javascript">
        function GetSpanElements () {
            var message = "";

            if (document.evaluate) {    // Firefox, Opera, Google Chrome and Safari
                var container = document.getElementById ("container");
                var xPathRes = document.evaluate ( 'span', container, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

                message += "The contents of the span elements:<br />";
                var actualSpan = xPathRes.iterateNext ();
                while (actualSpan) {
                    message += "<b>" + actualSpan.innerHTML + "</b><br />";
                    actualSpan = xPathRes.iterateNext ()
                }
            }
            else {  // Internet Explorer
                message = "Your browser does not support the evaluate method!";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="GetSpanElements ()">
    <span class="level1">Span 1</span>
    <div id="container">
        <span class="level2">Span 2</span>
        <div>
            <span class="level3">Span 3</span>
            <span class="level3">Span 4</span>
        </div>
        <span class="level2">Span 5</span>
        <span class="level2">Span 6</span>
    </div>
    <span class="level1">Span 7</span>
    <br /><br />
    <div id="info" style="background-color:#e0b0b0; width:400px"></div>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content