Translating a DOM to Other Collections

We've examined a simple Collections example, now we move on and examine how we can model the DOM as a Collection.

DOM to Array

The simplest way to provide the DOM as an array is to extend the ArrayList class. Listing 6.2 shows a very simple class that extends ArrayList and takes a document and a tag and creates an array collection, which can then be used anywhere a collection can be used.

The example itself is simple enough: Import java.util and then define the class to extend ArrayList (line 6). We then populate the array in the constructor as shown in lines 8–20. We can then use the class like any other collection and lines 33–39 simply exercise our new DOM-based collection.

Tip

A little trick you can ...

Get XML Development with Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.