Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of DOM NodeList returned by getChildNodes()

Tags:

java

dom

xml

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

For example, given <a><b/><c/><d/></a> is a.getChildNodes() guaranteed to return a NodeList with b, c and d in that order?

The javadoc isn't clear on this.

like image 572
Adrian Mouat Avatar asked Sep 15 '08 19:09

Adrian Mouat


People also ask

Is NodeList ordered?

The NodeList object represents an ordered list of nodes.

What is NodeList in Dom?

A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes). HTMLCollection items can be accessed by their name, id, or index number. NodeList items can only be accessed by their index number. An HTMLCollection is always a live collection.

Which of the following method returns the DOM node corresponding to the given element?

isSameNode(Node other) This method returns whether current node is the same node as the given one.

How do I iterate NodeList?

Note: Although NodeList is not an Array , it is possible to iterate over it with forEach() . It can also be converted to a real Array using Array. from() .


1 Answers

In my experience, yes. The DOM spec isn't any clearer. If you're paranoid, try something like

current = node.firstChild;
while(null != current) {
    ...
    current = current.nextSibling;
}
like image 107
sblundy Avatar answered Oct 20 '22 00:10

sblundy