Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't NodeList extend Collection or Iterable?

Perhaps this isn't exactly a programming question. But...

Why is org.w3c.dom.NodeList not an extension of java.lang.Iterable interface?

It sounds so counter-intuitive to me. Especially because the documentation says:

The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live. The items in the NodeList are accessible via an integral index, starting from 0.

PS: Please back your answers with proper citations where applicable.

like image 660
zEro Avatar asked May 07 '13 11:05

zEro


2 Answers

org.w3c.dom.NodeList predates Iterable, which was introduced in Java version 1.5.

Maybe it wasn't updated for compatibility reasons, but I have no references for this.

like image 52
Tony the Pony Avatar answered Nov 15 '22 13:11

Tony the Pony


The w3c only define specifications (XML, XSLT, DOM, etc...) and are not trying to align the API to any specific language or platform.

Its intended for developer of parsers as a guideline to produce a product compliant with existing code that uses these parsers.

When you build your application framework, it's best to wrap all API calls so you can control how the API is accessed in different language or on different platforms.

In Java, JavaScript, C# or whatever your using, create a class\object that wraps accessing the API calls. In JavaScript it will help when making code cross-browser compliant, if your publishing your solution for multiple platforms, you will only have to update your wrapper class.

Here is an example below, however, you can get as fancy as you want, define your own wrapper interface and base class with descendent classes overriding to provide specific implementations.

function XMLNode(xnode) {
  this.xnode = xnode;
}

function getNodes(path, xnode) {
  if (browseTYPE != IE) {

      //Ordered SnapShot
      if (xnode.evaluate)          
          fld = xnode.evaluate(path, xnode, null, 7, null);
      else
          fld = xnode.ownerDocument.evaluate(path, xnode, null, 7, null); 

      //We need a result wrapper here
      if (fld != null) return new XMLSnapShotList(fld); 

  } else {
      fld = xnode.selectSingleNode(path).childNodes;

      //We need a result wrapper here
      if (fld != null) return new XMLList(fld); 
  }
  return null;
}

XMLNode.prototype.getNodes = getNodes;
like image 34
Kyght Avatar answered Nov 15 '22 14:11

Kyght