[jdom-interest] why can't i doelementB.getContent().addAll(elementA.getContent())??

Noam Tamim noamt at yahoo.com
Sat Aug 24 02:26:53 PDT 2002


Didn't we once wrote such a thing? It's pretty simple. You use an Iterator:
approach a: a utility method - 
void moveContent(Element src, Element dst) {
 List srcList=src.getContent(), dstList=dst.getContent();
 Iterator it=srcList.iterator();
 while (it.hasNext())
 Object ob=it.next();
 it.remove();
 dstList.add(ob);
}

approach b would be to change ContentList.addAll(index,collection), so that if collection is a ContentList whose parent is NOT null, any object is removed from it (through the Iterator) before it is added to the destination list.

The first approach can be applied right away - just copy&paste (I didn't try to compile/run it though) - no changes to the code base needed.

Noam.

 Jason Hunter wrote:Yes, there should be a convenience method for that. It's my goal to see
one added.

-jh-

Phillip Rhodes wrote:
> 
> I want to move the content of Element A to Element B
> 
> Example
> 
> Element elementA = getElementMethod();
> Element elementB = new Element("newelement");
> List listA = elementA.getContent();
> elementB.getContent().addAll(listA);
> 
> The following will generate an error since the Elements, Text, CData
> objects have not been detached from the parent (elementA). While I know I
> can loop through the list, and test for the type of object it is and call
> the detach method, I wonder if there is a easier way. Don't you think
> there should be a convenience method somewhere to do this type of operation?



---------------------------------
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020824/fa374f3a/attachment.htm


More information about the jdom-interest mailing list