[jdom-interest] detach() [eg]

Jason Hunter jhunter at collab.net
Mon Apr 23 22:13:59 PDT 2001


Scott Means wrote:
> 
> I agree with Elliotte. I think having the parent detach its children is
> cleaner than requiring the children to be familiar with their parents
> (almost incestuous :) Also, and I don't know if this will win any points
> with this crowd, it does match how the W3C DOM API works:
> 
> Node removeChild(Node oldChild)
> Removes the child node indicated by oldChild from the list of children, and
> returns it.

If you look in Document you'll note there already is a removeContent()
method.  There's one in Element too.  But the problem is people ask,
"How do I move an element?" and it's REALLY nice to give the one-line
answer:

  elt.addContent(otherelt.detach());

Rather than have to give the 10-line answer you'd have to give without
it:

Element parent = otherelt.getParent();
if (parent != null) {
  parent.removeContent(otherelt);
}
else {
  // Might be a root element
  Document doc = otherelt.getDocument();
  if (doc != null) {
    doc.removeContent(otherelt);
  }
}
elt.addContent(otherelt);

-jh-



More information about the jdom-interest mailing list