madu
madu

Reputation: 5450

JAXB with 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.

Upvotes: 1

Views: 1246

Answers (2)

bdoughan
bdoughan

Reputation: 149017

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

Upvotes: 1

Amanpreet
Amanpreet

Reputation: 526

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

Upvotes: 1

Related Questions