Iteration can be implemented using two different approaches: using element
specific methods or using generic methods.
The first approach consists in defining specific document iteration
methods for each class. This approach is straightforward but difficult to
implement. In fact, some elements (e.g. Body ) contain a set of heterogeneous
components (e.g. Text and Link ). In order to iterate among them, the con-
tainer class must recognize their type. This can be done using the reflection
capability of the Java language. Unfortunately the use of reflection often
results in inelegant code.
The second approach consists in defining an abstract container class (e.g.
Element ) and to derive a concrete subclass for each element (see Figure 8.9).
The abstract container class defines a set of generic methods for document
iteration. This solution has been adopted by the World Wide Web
Consortium (W3C 2004c). For iteration on elements of an HTML page two
methods are defined in the common base class:
first Child , invoked on the container element, returns the first component;
nextSibling , invoked on a component element, returns the next compo-
nent inside the same container.
Figure 8.9 HTML object model