SV: [jdom-interest] moving an Element

Robert Koberg rob at koberg.com
Wed Jan 21 16:07:37 PST 2004


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