[jdom-interest] detach() [eg]
Elliotte Rusty Harold
elharo at metalab.unc.edu
Tue Apr 24 10:55:37 PDT 2001
At 10:13 PM -0700 4/23/01, Jason Hunter wrote:
>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);
I can do better than both of these :-)
Utilities.move(elementToBeMoved, newParentElement);
A method like this can of course encapsulate either the one line
version or the 10 line version.
We can overload both arguments quite heavily to take care of all the
different things we might want to move (Elements, Comments,
ProcessingInstructions, etc.). We could even add an optional third
argument to specify at what position in the new place we wanted the
moved thing put.
--
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| The XML Bible (IDG Books, 1999) |
| http://metalab.unc.edu/xml/books/bible/ |
| http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/ |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://metalab.unc.edu/javafaq/ |
| Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/ |
+----------------------------------+---------------------------------+
More information about the jdom-interest
mailing list