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

Robert Koberg rob at koberg.com
Wed Jan 21 19:08:44 PST 2004


Per Norrman wrote:

> Hi,
> 
> well, you could perhaps use a filter that hides the optional elements 
> in the getChildren call:
> 
>   List children = this.getElement(parentIdref).getChildren(filter);

I can't do that because folder/page are 'abstract' names they could be 
pretty much anything (e.g. newsfeed/volume or topic/content).

> 
> Or, better yet, perhaps you should build a proper object model, either
> as a facade directly in front of your JDOM tree, 

the moveElement is defined in an interface/implementation (e.g. 
Configuration/ConfigurationImpl) that is extended by some tree-managing 
interface/implementation (e.g. Site/SiteImpl or Topics/TopicsImpl), so I 
am trying to keep it generic.

I guess I am not sure what you are saying.

> or try a data binding
> solution (e.g. Castor or JAXB).

I have tried JAXB. It creates some extremely simple 
interfaces/implementations that do some of what I want, but it is too 
brittle for my needs. It requires a regeneration of those interf/impl's 
whenever the schema changes. And there could be different schemas for 
different clients in the same webapp.

I guess I will stick with my ugly method (incorporating some of your 
changes).

It is too bad that JDOM is not suited for this type of thing (and I 
don't want to use the DOM). I guess my needs are too specific.

Thanks for your response. If anybody else has some ideas, I am all 
ears/eyes :)

best,
-Rob

> 
> 
>>-----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>
>>
>>
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> 




More information about the jdom-interest mailing list