SV: SV: [jdom-interest] moving an Element

Per Norrman pernorrman at telia.com
Wed Jan 21 17:26:39 PST 2004


Hi,

well, you could perhaps use a filter that hides the optional elements 
in the getChildren call:

  List children = this.getElement(parentIdref).getChildren(filter);

Or, better yet, perhaps you should build a proper object model, either
as a facade directly in front of your JDOM tree, or try a data binding
solution (e.g. Castor or JAXB).

> -----Ursprungligt meddelande-----
> Från: Robert Koberg [mailto:rob at koberg.com] 
> Skickat: den 22 januari 2004 01:08
> Till: Per Norrman
> Kopia: jdom-interest at jdom.org
> Ämne: Re: SV: [jdom-interest] moving an Element
> 
> 
> Per Norrman wrote:
> 
> > 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);
> >     }	
> > 
> >  }
> 
> Thanks! That looks great. I did not like how mine looked 
> either -- thats 
> why I asked :)
> 
> But, I am realizing my question was too vague. I should have provided 
> some example XML. There is still one problem that will make the above 
> method not work in my case (and why I need[?] the apparently 
> unnecessary 
> operations). Below is some simplified, example XML that shows 
> the issue; 
> it is basically that some optional elements do not have IDs 
> but /should/ 
> retain the original order (see comments inline).
> 
> Using the XML below, how can I modify your code so that I can, for 
> example, insert a page element in /site/folder[@id='folder1'] *after* 
> the regions element?
> 
> thanks,
> -Rob
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <site id="site_root">
>    <label>Website</label>
>    <title>Website</title>
>    <!-- could also have a description elem
>         and/or a regions elem -->
> 
>    <page id="site_ind">
>      <label>index page</label>
>      <title>index page</title>
>      <regions>
>        <region name="wide_col">
>          <content ref="a123"/>
>        </region>
>      </regions>
>    </page>
> 
>    <folder id="folder1">
>      <label>folder 1</label>
>      <!-- title is optional -->
>      <title>folder 1</title>
>      <!-- description is optional -->
>      <description>blah blah</description>
>      <!-- regions is optional -->
>      <regions>
>        <region name="narrow_col">
>          <content ref="b123"/>
>        </region>
>      </regions>
> 
>      <page id="folder1_ind">
>        <label>index page</label>
>        <title>index page</title>
>        <regions>
>          <region name="wide_col">
>            <content ref="c123"/>
>          </region>
>        </regions>
>      </page>
> 
>      <page id="folder1_page">
>        <label>index page</label>
>        <title>index page</title>
>        <!-- no content assigned yet -->
>      </page>
>    </folder>
> </site>
> 
> 




More information about the jdom-interest mailing list