[jdom-interest] detach() [eg]

Jason Hunter jhunter at collab.net
Tue Apr 17 14:18:42 PDT 2001


Jools wrote:
> 
> Whilst working through the source code for Element.java I became
> uncomfortable
> with the detach() method.
> 
>     public Element detach() {
>         Element p = getParent();
>         if (p != null) {
>             p.removeContent(this);
>         }
>         else {
>             Document d = getDocument();
>             if (d != null) {
>                 d.setRootElement(new Element("placeholder"));
>             }
>         }
>         return this;
>     }
> 
> Which means that if we detach a root element from Document it ends
> up being a document with one root element <placeholder/>.
> 
> Do we really want this, or should we simply set the root Element to
> null ?

If you set it to null then you have a malformed document.  Every
well-formed document has a root element.  This was discussed earlier
actually.

We could be smart and look at the DocType to see if there's a "standard"
root name to use, but barring that we have to choose something.

Note that Xerces these days sometimes pre-creates root elements on new
docs, presumably for the same reason.

-jh-



More information about the jdom-interest mailing list