Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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 如何向@XmlElementWrapper ArrayList添加属性?_Java_Xml_Jaxb - Fatal编程技术网

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中进行注释