[jdom-interest] Resetting the Root Element

Laurent Bihanic laurent.bihanic at atosorigin.com
Wed Nov 7 01:28:29 PST 2007

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.


More information about the jdom-interest mailing list