[jdom-interest] Element.setAttribute(s)() B7 multiple bugs?
Matthew Stone
matt.stone at jcafeinc.com
Mon Jul 30 03:26:34 PDT 2001
I'm using JDOM B7 and hit a snag with the setAttribute(s)() methods.
First, I have a no namespace element called foo that contains several
attributes which are from multiple namespaces. For example:
<foo name="kk" xmlns:ns1="/xxx" ns1:att1="ABC" xmlns:ns2="/yyy"
ns2:att2="XYZ" />
Now in my business logic I copy the Element foo to foo2. Then I do some
processing thus changing the foo2 attributes. Once I'm done that I attempt
to synch up the foo and foo2 attributes. This is where I hit the snag.
Initially I tried doing the attribute copy this way:
foo.setAttributes(foo2.getAttributes());
The results caused the "name='kk'" attribute to be duplicated in the foo
element. Every time I ran my business process an additional name="kk" will
be added.
So to work around this bug I tried this:
java.util.Iterator it = foo2.getAttributes().iterator();
while( it.hasNext() ){
org.jdom.Attribute at = (org.jdom.Attribute)it.next();
foo.setAttribute( at );
}
This caused an Exception to be thrown stating Illegal..Something. The
detailed message said the foo2 attribute could not be set because the
attribute already exist on foo.
I finally got it to work by doing it this way:
java.util.Iterator it = foo2.getAttributes().iterator();
while( it.hasNext() ){
org.jdom.Attribute at = (org.jdom.Attribute)it.next();
foo.setAttribute( at.getName(), at.getValue(), at.getNamespace() );
}
Of course the most logical solution for me was what I tried first...
foo.setAttributes(foo2.getAttributes());
Was I wrong to expect it to create/overwrite the attributes on foo2? Per
the doc's, B7 was supposed to provide support for this right?
Regards,
Matthew
More information about the jdom-interest
mailing list