Order of elements from minidom getElementsByTagName
<p>Is the order for returned elements from Mindom <code>getElementsByTagName</code> the same as it is in document for elements in the same hierarchy / level?</p>

<pre><code>    images = svg_doc.getElementsByTagName('image') 
    image_siblings = []
    for img in images:
        if img.parentNode.getAttribute('layertype') == 'transfer':
            if img.nextSibling is not None:
                if img.nextSibling.nodeName == 'image':
                    image_siblings.append(img.nextSibling)
                elif img.nextSibling.nextSibling is not None and img.nextSibling.nextSibling.nodeName == 'image':
                    image_siblings.append(img.nextSibling.nextSibling)
</code></pre>

<p>I need to know if <code>image_siblings</code> will contain the images in the same order, they are placed in the document for the same hierarchy.</p>

<p>I found a similar <a href="https://stackoverflow.com/questions/4954003/order-of-the-elements-returned-using-getelementsbytagname">question</a> for JavaScript, but I'm unsure if this is also true for Python (version 3.5.2) Minidom <code>getElementsByTagName</code>.</p>

<p>According to the code (in Python 2.7), the <code>getElementsByName</code> method relays on the <code>_get_elements_by_tagName_helper</code> function, which code is:</p>

<pre><code>def _get_elements_by_tagName_helper(parent, name, rc):
    for node in parent.childNodes:
        if node.nodeType == Node.ELEMENT_NODE and \
            (name == "*" or node.tagName == name):
            rc.append(node)
        _get_elements_by_tagName_helper(node, name, rc)
    return rc
</code></pre>

<p>What this means is that the order in the <code>getElementByName</code> is the same that you have in the <code>childNodes</code>.</p>

<p>But this is true only if the <code>tagName</code> appears only in the same level. Notice the recursive call of <code>_get_elements_by_tagName_helper</code> inside the same function, which means that elements with the same <code>tagName</code> that are placed deeper in the tree will be interleaved with the ones you have in a higher level.</p>

<p>If by <em>document</em> you mean an XML text file or a string, the question is then moved to whether or not the parser respects the order when creating the elements in the DOM. 
If you use the <code>parse</code> function from the <code>xml.dom.minidom</code>, it relays on the <code>pyexpat</code> library, that in turns use the <code>expat</code> C library.</p>

<p>So, the short answer would be:</p>

<blockquote>
  <p>If you have the tagName only present in the same level of hierarchy in the XML DOM, then the order is respected. If you have the same tagName in other nodes deeper in the tree, those elements will be interleaved with the ones of higher level. The respected order is the order of the elements in the minidom document object, which order depends on the parser.</p>
</blockquote>

<p>Look this example:</p>

<pre><code>&gt;&gt;&gt; import StringIO
&gt;&gt;&gt; from xml.dom.minidom import parseString
&gt;&gt;&gt; s = '''&lt;head&gt;
...   &lt;tagName myatt="1"/&gt;
...   &lt;tagName myatt="2"/&gt;
...   &lt;tagName myatt="3"/&gt;
...   &lt;otherTag&gt;
...     &lt;otherDeeperTag&gt;
...       &lt;tagName myatt="3.1"/&gt;
...       &lt;tagName myatt="3.2"/&gt;
...       &lt;tagName myatt="3.3"/&gt;
...     &lt;/otherDeeperTag&gt;
...   &lt;/otherTag&gt; 
...   &lt;tagName myatt="4"/&gt;
...   &lt;tagName myatt="5"/&gt;
... &lt;/head&gt;'''
&gt;&gt;&gt; doc = parseString(s)
&gt;&gt;&gt; for e in doc.getElementsByTagName('tagName'):
...     print e.getAttribute('myatt')
... 
1
2
3
3.1
3.2
3.3
4
5
</code></pre>

<p>It seems the parser respects the ordering structure of the xml string (most parsers respect that order because it is easier to respect it) but I couldn't find any documentation that confirms it. I mean, it could be the (strange) case that the parser, depending on the size of the document, moves from using a list to a hash table to store the elements, and that could break the order. Take into account that the XML standard does not specify order of the elements, so a parser that does not respect order would be complaint too.</p>

python xml python-3.x dom minidom