I have a problem with my JAXB. I have a method with @XmlAnyAttribute (on my getter) but it doesn’t seem to work with the setter (using JAXB RI if it matters).
Simplified code:
@XmlRootElement( name = "element" )
@XmlAccessorType( value = XmlAccessType.PUBLIC_MEMBER )
public class Element
{
private Map<QName, String> convertedAttributes = new HashMap<QName, String>();
private List<Attribute> attributes = new ArrayList<Attribute>();
@XmlAnyAttribute
public Map<QName, String> getConvertedAttributes() throws Exception
{
if ( attributes != null )
{
return new AttributeMapAdapter().marshal( attributes );
}
return new HashMap<QName, String>();
}
public void setConvertedAttributes( Map<QName, String> convertedAttributes )
{
this.convertedAttributes = convertedAttributes;
}
@XmlTransient
public List<Attribute> getAttributes()
{
return attributes;
}
public void setAttributes( List<Attribute> attributes )
{
this.attributes = attributes;
}
}
This work great for marshalling, and I get the output I want. But when I try to unmarshall it, no values it sent to the setter.
I tried moving the @XmlAnyAttribute annotation to the field, and it works fine (but then I can’t do the adaption in the getter).
It kinda feels like a bug, but I’m not sure. Any ideas? I’m using Java 1.6 on Mac OS X (10.7.2)
This isn’t a bug in the JAXB RI. The problem is in your
getConvertedAttributes()method. The following works a bit better: