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.
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.
精彩评论