[jdom-interest] addAttribute

Trimmer, Todd todd.trimmer at trizetto.com
Tue Feb 20 16:54:34 PST 2001


Why is an exception thrown when one calls Element.addAttribute() twice with
the same Attribute? Why can't the newer Attribute simply replace the old
one, if not already there, like with DOM?

A solution would be to add setAttribute( Attribute attr ) and setAttribute(
String, String ) to Element. Allow these methods to add the specified
Attribute to the Element if no there, and to replace the Attribute if it is
there.

Otherwise I'm forced to do:

	elm.removeAttribute( attrName );
	elm.addAttribute( attrName, attrVal );

or

	elm.removeAttribute( attr.getName(), attr.getNamespace() );
	elm.addAttribute( attr );

every time if I wish to code in an "add and forget" mode. I don't understand
why you want to impose the cost of instantiating a new exception object,
simply because the Attribute is already there. You are already performing
the cost of a lookup, so why not just modify the search result?

Todd Trimmer




More information about the jdom-interest mailing list