import javax.xml.bind.annotation.*;
@XmlRootElement
public class Student {
@XmlElement
private String name;
@XmlElement
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
import javax.xml.bind.*;
import java.io.*;
public class JAXBExample {
public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(student, writer);
String xml = writer.toString();
System.out.println(xml);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
Student deserializedStudent = (Student) unmarshaller.unmarshal(reader);
System.out.println(deserializedStudent.getName());
System.out.println(deserializedStudent.getAge());
}
}