@XmlRootElement
public class Student {
@XmlElement
private String name;
@XmlElement
private int age;
}
public class JAXBExample {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Student student = new Student();
student.setAge(18);
marshaller.marshal(student, System.out);
}
}
<Student>
<age>18</age>
</Student>
public class JAXBExample {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
FileReader fileReader = new FileReader("student.xml");
Student student = (Student) unmarshaller.unmarshal(fileReader);
}
}