Hey Tob
Sorry for the delay in getting back to this one - I've been a bit snowed
under with email here at JavaOne and I nearly missed this message.
> does the current API support it to inserting a element and its children
> after the current element? The add Method of Element inserts a new child
to it,
> doesn't it!
>
> Could't it be usefull to provide a instertAfter(Element element) Method in
> Element (and its conrete impleantion AbstractElement).
>
> Currently I use java.util.List to achive that:
>
> Element parent = currentElement.getParent();
> List list = parent.elements();
>
> /*
> * size()-1 is ugly but prevents ArrayOutOfBounds because size() is
> always +1
> * as reale size
> */
> List subList = list.subList(parent.indexOf(currentElement)+1,
> list.size()-1);
> list.removeAll(subList);
> list.add(elment);
> list.addAll(list.indexOf(dolly)+1, subList);
>
> My question now: 1) Is there a easier way to achive that?
You could do it as follows:-
Element parent = currentElement.getParent();
int index = parent.indexOf( currentElement );
List list = parent.content();
list.add( index + 1, element );
Which allows the newElement to be added after the currentElement.
> 2) If not could we
> add insertAfter(Element element) to the API ?
I think there may be too many combinations to deal with - adding at a
specified index, adding after or before a specified sibling node.
Its easy to find the index of something as in the above example code using:-
parent.indexOf(currentElement )
But I am kinda tempted to support adding of a new Element or Text node at a
specified index. So you could do something like this...
Element parent = currentElement.getParent();
int index = parent.indexOf( currentElement );
Element newAfterElement = parent.add( index + 1, "foo" );
Element newBeforeElement = parent.add( index, "foo" );
Would that seem a good idea to people on the list?
James
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user