The following example shows unmarshalling an XML using default values.
We can specify the default values using defaultValue attribute of
@XmlElement annotation.
public static void main (String [] args) throws JAXBException {
/**
* Create JAXB Context from the classes to be serialized
*/
JAXBContext context = JAXBContext.newInstance(Student7.class);
Unmarshaller um = context.createUnmarshaller();
StringReader sr = new StringReader(
"<student7>" +
"<firstName>Sriram</firstName>" +
"<lastName/>" +
"<age></age>" +
"</student7>");
Student7 std = (Student7) um.unmarshal(sr);
System.out.println("Last Name : " + std.getLastName()); //Empty tag takes default value
System.out.println("Age : " + std.getAge()); //Empty tag takes default value
System.out.println("Hobby : " + std.getHobby()); //No tag doesn't take default value