Java 如何向@XmlElementWrapper ArrayList添加属性?
我正在使用Java 如何向@XmlElementWrapper ArrayList添加属性?,java,xml,jaxb,Java,Xml,Jaxb,我正在使用jaxb创建以下xml结构: @XmlElementWrapper @XmlElement(name = "picture") private List<Picture> pictures; @xmlementwrapper @xmlement(name=“picture”) 私人列表图片; 创建: <pictures> <picture/> <picture/> </pictures> 我想要的是:
jaxb
创建以下xml结构:
@XmlElementWrapper
@XmlElement(name = "picture")
private List<Picture> pictures;
@xmlementwrapper
@xmlement(name=“picture”)
私人列表图片;
创建:
<pictures>
<picture/>
<picture/>
</pictures>
我想要的是:
<pictures attribute="test">
<picture/>
<picture/>
</pictures>
问题:我怎样才能做到这一点?为此,您应该创建一个这样的类
@XmlRootElement(name = "pictures")
class YourClass{
@XmlElement(name = "picture")
private List<Picture> pictures;
@XmlAttribute(name="attribute")
private string attr;
}
@XmlRootElement(name=“pictures”)
你的班级{
@xmlement(name=“picture”)
私人列表图片;
@xmldattribute(name=“attribute”)
私有字符串属性;
}
当你看到这个的时候
图片将是您的根元素,其中我们有一个属性
属性和其他列表元素(图片)在根元素中可用
你会得到
<pictures attribute="test">
<picture/>
<picture/>
</pictures>
注意:您也可以在getters中进行注释。为此,您应该创建一个这样的类
@XmlRootElement(name = "pictures")
class YourClass{
@XmlElement(name = "picture")
private List<Picture> pictures;
@XmlAttribute(name="attribute")
private string attr;
}
@XmlRootElement(name=“pictures”)
你的班级{
@xmlement(name=“picture”)
私人列表图片;
@xmldattribute(name=“attribute”)
私有字符串属性;
}
当你看到这个的时候
图片将是您的根元素,其中我们有一个属性
属性和其他列表元素(图片)在根元素中可用
你会得到
<pictures attribute="test">
<picture/>
<picture/>
</pictures>
注意:您也可以在getter中进行注释