import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
@XmlAttribute
private int id;
@XmlElement
private String name;
// Getters and setters
}
import javax.xml.bind.*;
public class Main {
public static void main(String[] args) throws Exception {
Student student = new Student();
student.setId(1);
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(student, System.out);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Student student2 = (Student) unmarshaller.unmarshal(new File("student.xml"));
System.out.println(student2.getName());
}
}