[jdom-interest] Re: Detaching root elements

Dave Churchville dmc at chimerasoft.com
Mon Apr 23 09:50:39 PDT 2001


It seems to me that the most common reasons for detaching a root element
are:

1. I'd like to place it as a child of some other element (that doesn't start
with the same root).  This is very common in a situation where I'm merging
documents.  I really dont' want to special case this code, catch funny
exceptions, etc. here.  To keep it well formed, instead of the old document
having a magic placeholder, better that it create a new root element
containing whatever was in the old root element before detaching.  This will
keep it well-formed, and could be more easily explained.

2. I'm trying to "clean up" a document by removing all children.  Here I'm
semantically trying to "clear" the Document and allow its children elements
to be garbage-collected.  The big objection so far has been "but then my
Document wouldn't be well-formed".   This isn't even a particularly
compelling use case, expect when I'm trying to "keep" all processing
instructions, etc., and "reuse" the document with other root elements.  In
this case, we do have setRootElement().  No special exceptions, etc. needed.

3. I have some generic tool or application that can call detach() on any
element, just to remove it.  Here I can certainly check if the element
isRootElement() to handle any special cases.




More information about the jdom-interest mailing list