The following example shows serializing a POJO to an XML file.
The POJO should have a no-arg default constructor and
@XmlRootElement annotation at class level.
public static void main (String [] args) throws JAXBException, FileNotFoundException {
Student student = new Student("Sriram", "Kasireddi", 2, "Painting", new Date());
/**
* Create JAXB Context from the classes to be serialized
*/
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(student, new FileOutputStream("Student.xml"));
}
}