import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import java.io.File;
public class Student {
private String name;
private int age;
}
public class JAXBExample {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Student student = new Student();
student.setName("Alice");
student.setAge(20);
marshaller.marshal(student, System.out);
Unmarshaller unmarshaller = context.createUnmarshaller();
File xmlFile = new File("student.xml");
Student studentFromXml = (Student) unmarshaller.unmarshal(xmlFile);
System.out.println("Name: " + studentFromXml.getName());
System.out.println("Age: " + studentFromXml.getAge());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
<student>
<name>Alice</name>
<age>20</age>
</student>