gpt4 book ai didi

java - @XmlAnyAttribute 和方法有错误吗? (使用 JAXB RI)

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:06 26 4
gpt4 key购买 nike

我的 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;
}
}

这非常适合编码,我得到了我想要的输出。但是当我尝试解码它时,它没有向 setter 发送任何值。

我尝试将 @XmlAnyAttribute 注释移至该字段,它工作正常(但随后我无法在 getter 中进行适应)。

感觉像是一个错误,但我不确定。有任何想法吗?我在 Mac OS X (10.7.2) 上使用 Java 1.6

最佳答案

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

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;
}

关于java - @XmlAnyAttribute 和方法有错误吗? (使用 JAXB RI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com