Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

SV: [jdom-interest] moving an Element

Per Norrman

2004-01-21

Replies:

Hi,

What I think about it? It was hard to follow, contained unnecessary
operations
and did not guarantee that the element was moved.


public final void moveElement(Element elem, String parentIdref, String
prevSiblIdref) {
  String focusIdref = elem.getAttributeValue("id");
  elem.detach();

  if ("null".equals(prevSiblIdref)) {
   children.add(0, elem);
 return;
  }

  List children = this.getElement(parentIdref).getChildlen();

  Element prev = this.getElement(prevSiblIdref);
  int index = children.indexOf(prev);
  if (index == -1) {
    children.add(elem);
  } else {
  children.add(index+1, elem);
  }  

}

> -----Ursprungligt meddelande-----
> Från: jdom-interest-admin@(protected)
> [mailto:jdom-interest-admin@(protected)
> Skickat: den 21 januari 2004 17:37
> Kopia: jdom-interest@(protected)
> Ämne: [jdom-interest] moving an Element
>
>
> Hi,
>
> What is the best way to move an org.jdom.Element with jdom?
>
> I am using the method below. What do you think about it?
>
> public final void moveElement(Element elem, String
> parentIdref, String
> prevSiblIdref) {
>   String focusIdref = elem.getAttributeValue("id");
>   Element parent = this.getElement(parentIdref);
>   List children = parent.getChildren();
>   List _children = new ArrayList(children.size() + 1);
>   for (ListIterator i = children.listIterator(); i.hasNext();) {
>    Element child = (Element) i.next();
>
>    String id = child.getAttributeValue("id");
>    if (null == id) {
>     _children.add(child);
>    } else if (!id.equals(focusIdref)) {
>     _children.add(child);
>    }
>
>    if (null != id && id.equals(prevSiblIdref)) {
>     _children.add(elem);
>    }
>   }
>   if ("null".equals(prevSiblIdref)) {
>    _children.add(0, elem);
>   }
>   children.clear();
>   parent.setContent(_children);
> }
>
> thanks,
> -Rob
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr@(protected)

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.