The following example shows serializing a POJO to an XML.
The POJO should have a no-arg default constructor and
@XmlRootElement annotation at class level.
The propOrder attribute of
@XmlType annotation allows us to
order the tags in generated XML output.
public static void main (String [] args) throws JAXBException {
Student2 student = new Student2("Sriram", "Kasireddi", 2, "Painting", new Date());
/**
* Create JAXB Context from the classes to be serialized
*/
JAXBContext context = JAXBContext.newInstance(Student2.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(student, System.out);