[jdom-interest] Rootless documents

Bradley S. Huffman hip at a.cs.okstate.edu
Mon Apr 8 15:09:13 PDT 2002

Elliotte Rusty Harold writes:

> Can somebody remind me what the use-case for rootless documents was? I'm 
> working on the JDOM chapters of Processing XML with Java and I'm having 
> trouble justifying this to my readers.

Mainly in building a document. SAXBuilder is one example where it creates
a new Docuement (in startDocument), then adds content as it is found.
Saves having to store a Document's content then create it at the end of

For normal use about all it does is allow:

    Document doc = new Document();
    doc.addContent( new Comment("first comment"));
    doc.addContent( new Element("test"));
    doc.addContent( new Comment("last comment"));

instead of say:

    List list = new Vector();
    list.add( new Comment("first comment"));
    list.add( new Element("test"));
    list.add( new Comment("last comment"));
    Document doc = new Document(list);

Both of produce the same results.  One safe guard in the rootless version is
until you create a legal document you cann't do anything with it (except
set the DocType and add content) till it is legal, i.e. getContent,
getRootElement will throw a ISE and tell you the root element is not set.

It's really not needed since there are other ways to do the same thing, but
java coders are so use to creating empty lists and such, and then adding
items to it, this seems a natural thing to have.


More information about the jdom-interest mailing list