Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.w3c.dom.NodeList doesn't extend Iterable

Tags:

java

dom

Is there any reason why would authors of Java org.w3c.dom library choose not to support the Iterable interface? For example, the interface NodeList seems like a perfect fit for extending Iterable.

like image 246
prasopes Avatar asked Oct 02 '22 05:10

prasopes


1 Answers

The World Wide Web consortium has defined the Document Object Model (DOM) as follows:

The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.

It's implementation for a number of languages look very much like each other, which smart people thought to be a good idea, a lot of years ago when they designed it. As a result, it doesn't look like anything familiar in any language.

If you want to use an alternative to the w3c DOM that does look like a Java library, use JDOM. Or map your XML to Java objects using a mapping/binding solution, such as JAXB

But if you need to interface with existing libraries that already use w3c DOM (like the built-in XSLT and XSD processors), then you're stuck with it. Unfortunately.


To @eis:

Yes there is a reason that you can't add an interface such as Iterable to NodeList, and that reason is that the Java binding of the Document Object Model is defined in the standard. Take NodeList, it is 100% defined in the standard. No room for any extra interfaces.

org/w3c/dom/NodeList.java:

package org.w3c.dom;

public interface NodeList {
    public Node item(int index);

    public int getLength();

}

There is no binding in the standard for C#, but there is one for EcmaScript. I believe the the IXMLDocument interfaces that you mention are also used for their EcmaScript implementation (but I could be wrong), in which case they still need to stick to the standard in terms of what methods they support and what the type hierarchy is.

The difference is that the EcmaScript binding only describes which methods should exist, while the Java binding describes the exact method in the interface. There is no reason though in Java that the class that implements NodeList can't implement Iterable too. However, if your code depended on that it would not work with the DOM standard, but with a particular implementation only.

Microsoft has never really bothered with this fine distinction since they generally don't cater for multiple standards compliant implementations - if you use any of the methods that Microsoft has labelled with "* Denotes an extension to the World Wide Web Consortium (W3C) DOM." in Microsoft's implementation, then you're not using the DOM standard.

like image 50
Erwin Bolwidt Avatar answered Oct 05 '22 11:10

Erwin Bolwidt