Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @XmlAnyAttribute和方法的Bug?(使用JAXB RI)_Java_Jaxb - Fatal编程技术网

Java @XmlAnyAttribute和方法的Bug?(使用JAXB RI)

Java @XmlAnyAttribute和方法的Bug?(使用JAXB RI),java,jaxb,Java,Jaxb,我的JAXB有问题。我有一个带有@XmlAnyAttribute的方法(在我的getter上),但它似乎不适合setter(如果有必要的话,使用JAXB-RI) 简化代码: @XmlRootElement( name = "element" ) @XmlAccessorType( value = XmlAccessType.PUBLIC_MEMBER ) public class Element { private Map<QName, String> convertedAt

我的JAXB有问题。我有一个带有@XmlAnyAttribute的方法(在我的getter上),但它似乎不适合setter(如果有必要的话,使用JAXB-RI)

简化代码:

@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;
    }
}
@XmlRootElement(name=“element”)
@XmlAccessorType(值=XmlAccessType.PUBLIC_成员)
公共类元素
{
private Map ConvertedAttribute=new HashMap();
私有列表属性=新的ArrayList();
@XmlAnyAttribute
公共映射GetConvertedAttribute()引发异常
{
如果(属性!=null)
{
返回新属性mapadapter().marshal(属性);
}
返回新的HashMap();
}
公共void集合ConvertedAttribute(映射ConvertedAttribute)
{
this.convertedAttributes=convertedAttributes;
}
@XmlTransient
公共列表getAttributes()
{
返回属性;
}
公共void集合属性(列表属性)
{
this.attributes=属性;
}
}
这项工作对于编组非常有用,我得到了想要的输出。但是当我试图解组它时,它没有向setter发送任何值

我尝试将@XmlAnyAttribute注释移动到字段中,效果很好(但是我无法在getter中进行自适应)


有点像虫子,但我不确定。有什么想法吗?我正在Mac OS X(10.7.2)上使用Java 1.6

您的setter必须再次解组映射。因此,您还需要另一个方向的适配器。

您的setter必须再次解组地图。因此,您还需要另一个方向的适配器。

这不是JAXB RI中的错误。问题出在您的
getConvertedAttribute()
方法中。以下方法效果更好:

public Map<QName, String> getConvertedAttributes() throws Exception
{
    if(!convertedAttributes.isEmpty()) {
        return convertedAttributes;
    }
    if ( attributes != null ) {
        convertedAttributes = new AttributeMapAdapter().marshal( attributes ); 
    } else {
        convertedAttributes = new HashMap<QName, String>();
    }
    return convertedAttributes;
}
公共映射GetConvertedAttribute()引发异常
{
如果(!convertedAttributes.isEmpty()){
返回转换属性;
}
如果(属性!=null){
convertedAttributes=新属性maPadApter().marshal(属性);
}否则{
convertedAttributes=新HashMap();
}
返回转换属性;
}

这不是JAXB RI中的错误。问题出在您的
getConvertedAttribute()
方法中。以下方法效果更好:

public Map<QName, String> getConvertedAttributes() throws Exception
{
    if(!convertedAttributes.isEmpty()) {
        return convertedAttributes;
    }
    if ( attributes != null ) {
        convertedAttributes = new AttributeMapAdapter().marshal( attributes ); 
    } else {
        convertedAttributes = new HashMap<QName, String>();
    }
    return convertedAttributes;
}
公共映射GetConvertedAttribute()引发异常
{
如果(!convertedAttributes.isEmpty()){
返回转换属性;
}
如果(属性!=null){
convertedAttributes=新属性maPadApter().marshal(属性);
}否则{
convertedAttributes=新HashMap();
}
返回转换属性;
}

如果需要,我可以提供更多代码。如果需要,我可以提供更多代码。我有一个用于该方向的映射器。但是ConvertedAttribute总是空的,它不会得到任何值。在字段上使用@XmlAnyAttribute可以给我解组的值,但在方法上使用它不起作用。@mortenoh是的,你是对的,它将只使用getter来访问基础映射。您必须装饰返回的getter映射,并在那里拦截put和get方法。我建议让您当前的适配器实现映射接口并返回它。我有一个用于该方向的映射器。但是ConvertedAttribute总是空的,它不会得到任何值。在字段上使用@XmlAnyAttribute可以给我解组的值,但在方法上使用它不起作用。@mortenoh是的,你是对的,它将只使用getter来访问基础映射。您必须装饰返回的getter映射,并在那里拦截put和get方法。我建议让您当前的适配器实现map接口并返回它。