[jdom-interest] Element.addAttribute() not replacing Attributes.

Robert R. Sanders robert.sanders at ipov.net
Wed Mar 28 09:40:53 PST 2001

I had a few problems with Element.addAttribute() in that it didn't update/replace old attributes, I looked at the latest source from the CVS snapshot and the problem was still there.  I took a few minutes and poked around and here is what I came up with (I just tested for my simple case, but it seemed to work).  I simply call removeAttribute() if the attribute already exists.  Maybe someone will verify this, and if it works check it in.

Robert r. Sanders

  * <p>
  * This adds an attribute to this element.  Any existing attribute with
  * the same name and namespace URI is removed.
  * </p>
  * @param attribute <code>Attribute</code> to add
  * @return this element modified
 public Element addAttribute(Attribute attribute) {
  if (getAttribute(attribute.getName(), attribute.getNamespace()) != null) {
   if (! removeAttribute(attribute.getName(), attribute.getNamespace() ) ) {
    throw new IllegalAddException(this, attribute, "Duplicate attributes are not allowed, unable to remove old Attribute.");

  if (attribute.getParent() != null) {
   throw new IllegalAddException(this, attribute,
    "The attribute already has an existing parent \"" +
    attribute.getParent().getQualifiedName() + "\"");

  // XXX Should verify attribute ns prefix doesn't collide with
  // another attribute prefix or this element's prefix

  if (attributes == null) {
   attributes = new LinkedList();

  return this;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010328/7be2e63c/attachment.htm

More information about the jdom-interest mailing list