Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

Re: [jdom-interest] Resetting the Root Element

Laurent Bihanic

2007-11-07

Replies:


Jason Hunter wrote:
>> You have to create a new root element and detach content from the old
>> root to add it to the new root :
>>   Element newRoot = new Element(...);
>>   newRoot.setContent(oldRoot.removeContent());
>>
>> Unfortunately, there is no Element.removeAttributes() method. So
>> you'll probably have to iterate on the old root's attribute list to
>> detach each attribute (using Iterator.remove()) and add it to the new
>> root.
>> Any better way, someone ?
>
> You can do elt.setAttributes(null).
> (Or pass an empty list.)

Yes but the interest of removeContent() in the present case is that it returns
the list of the detached nodes. This is not the case for setAttributes() which
returns the current element; the detached attributes are then lost.
I was looking for a single-line-of-code attribute transfer, just as I can do
with content.

Laurent

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.