<p>The DOM method <code>getChildNodes()</code> returns a <code>NodeList</code> of the children of the current <code>Node</code>. Whilst a <code>NodeList</code> is ordered, is the list guaranteed to be in document order?</p>

<p>For example, given <code>&lt;a&gt;&lt;b/&gt;&lt;c/&gt;&lt;d/&gt;&lt;/a&gt;</code> is <code>a.getChildNodes()</code> guaranteed to return a <code>NodeList</code> with <code>b</code>, <code>c</code> and <code>d</code> <em>in that order</em>?</p>

<p>The <a href="http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()" rel="noreferrer">javadoc</a> isn't clear on this.</p>

[{'answer_id': 66112, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 2, 'selected': False, 'text': "<p>A document-ordered node list is the behavior in other implementations of the DOM, such as Javascript's or Python's. And a randomly-ordered node list would be utterly useless. I think it's safe to depend on nodes being returned in document order.</p>\n"}, {'answer_id': 66116, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 4, 'selected': True, 'text': '<p>In my experience, yes. The <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1451460987" rel="nofollow noreferrer">DOM spec</a> isn\'t any clearer. If you\'re paranoid, try something like</p>\n\n<pre><code>current = node.firstChild;\nwhile(null != current) {\n    ...\n    current = current.nextSibling;\n}\n</code></pre>\n'}, {'answer_id': 66146, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 2, 'selected': False, 'text': "<p>My experience is that every time that I have bothered to look it has been in document order. However, I believe that I read somewhere it is not guaranteed to be in document order. I can't find where I read that right now, so take it as hearsay. I think your best bet if you <strong>must</strong> have them in document order would be to use FirstChild then NextSibling until there are no more sibs.</p>\n"}, {'answer_id': 66157, 'author': 'Calum', 'author_id': 8434, 'author_profile': 'https://Stackoverflow.com/users/8434', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d love to tell you that this is guaranteed (as I believe it is) but the <a href="http://www.w3.org/DOM/" rel="nofollow noreferrer">Document Object Model specification</a> itself seems ambiguous in this case. I\'m pretty sure that it\'s always document-order, though.</p>\n'}, {'answer_id': 66160, 'author': 'BrewinBombers', 'author_id': 5989, 'author_profile': 'https://Stackoverflow.com/users/5989', 'pm_score': 0, 'selected': False, 'text': "<p>In your example, as presented. I believe so. However, I've experienced real-world experiences where spaces have been interpreted as nodes so:</p>\n\n<p><pre><code>\n&lt;a&gt;&lt;b/&gt;&lt;c/&gt;&lt;d/&gt;&lt;/a&gt;\n</pre></code></p>\n\n<p>is different than</p>\n\n<p><pre><code>\n&lt;a&gt;&lt;b/&gt; &lt;c/&gt;&lt;d/&gt;&lt;/a&gt;\n</pre></code></p>\n\n<p>if you're looking at index [1], firefox and IE may present different results. I would advise against relying on the order depending on your need.</p>\n"}, {'answer_id': 4294653, 'author': 'houston', 'author_id': 522633, 'author_profile': 'https://Stackoverflow.com/users/522633', 'pm_score': 0, 'selected': False, 'text': '<p>Yes they are ordered as it returns a nodeList, you would have to say getNamedChildNodes to get a list that is not ordered as in namedNodeList.</p>\n'}]
['https://Stackoverflow.com/questions/66032', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4332/']