[jdom-interest] Setting / Adding Elements "in between"

ecky at free.fr ecky at free.fr
Mon Aug 20 08:46:42 PDT 2001


Hi there,

I wonder what is the destined way to insert / replace 
Child Elements of an element. I tried to get the list
of elements (using getMixedContent) add/replace an
element to/of it at the position I like, and try to
use setMixedContent again for the changes to take
effect - which throws an exception for the elements
that were allready present. As I'm working on a lifelist
I imagine the reason why. Then I read in an old mail
of Jason that I just should add the element to the
life list and that's it (see full message at the end
of this mail). This arises my question: Will this not
lead to problems later on, as I don't see how the
parent of the element inserted is being set?

A work around would be to add it to the end and swap
it afterwards to the place I'd like but ... hmmm this
appears to lack some elegance ;-).

Did I get something wrong? In case not, might it be
a good Idea to add a method setXXX( int index, XXX Item )
for each XXX that has to have it's parent set correctly?

I know that the API of Element is already quite large,
but ... hmmm 

Oh, BTW I'm using pre-beta0.7 loaded from CVS on 20.06.2001
I'll try with the latest version as well


cheers
ecky


--------------------------------------------------------------------------------
Summarize by: Date  Author  Subject  Thread    Search  
Description: jdom-interest at jdom.org (See other lists)
Subject: [jdom-interest] inserting a child element as the first, or (nth) 
element ... 
Options: Browse list | Browse messages with this subject | Browse messages by 
this author  Previous message: [jdom-interest] Getting two copies of every 
element (Troy Shahoumian) 
Next message: [jdom-interest] Getting two copies of every element (Jason 
Hunter) 
 
Jason Hunter (jhunter at collab.net) writes on 2000-08-13 (display as raw 
message)  
Date: Sun, 13 Aug 2000 22:58:56 -0700
From: Jason Hunter <jhunter at collab.net> 
Subject: [jdom-interest] inserting a child element as the first, or (nth)
 element ...


 
> Alright, I give up... what's the easiest way to insert an element 
> somewhere other than at the end of the list of children??

List kids = element.getParent().getChildren();
kids.add(5, newKid);
// etc

You can use getMixedContent() instead of getChildren() if you want to
worry about comments, entities, and other "node" placements.

-jh-






More information about the jdom-interest mailing list