开发者

JAXB with variable number of @XMLElements

开发者 https://www.devze.com 2023-04-05 12:51 出处:网络
Following the tutorial given in http://blog.adamsbros.org/2010/02/07/jaxb-example-code/ I\'d like to know if it is 开发者_开发技巧possible to have variable number of XMLElements.

Following the tutorial given in http://blog.adamsbros.org/2010/02/07/jaxb-example-code/ I'd like to know if it is 开发者_开发技巧possible to have variable number of XMLElements. For example, my class will be:

@XmlRootElement(name = "employee")
public class Teacher {

@XmlAttribute
String TeacherName;

@XmlElement
List<String> StudentNames = new ArrayList<String>();
}

I would like JAXB to create a XML such as:

<Teacher TeacherName="Mary">
 <StudentName>John</StudentName>
 <StudentName>Paul</StudentName>
</Teacher>

Is this possible to have variable number of elements with JAXB or is there a better way to handle something like this? Any help is appreciated.

Thank you.


Below I have modified the metadata you provided in your question to match your desired XML document.

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Teacher {

    @XmlAttribute(name="TeacherName")
    String TeacherName;

    @XmlElement(name="StudentName")
    List<String> StudentNames = new ArrayList<String>();

}

For More Information

  • http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html


Well if you are concerned about getting the size then we have size() method in ArrayList class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号