<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
public class Student {
private String name;
private int age;
private String className;
}
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import java.io.File;
public class XMLToObjectConverter {
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
File xmlFile = new File("student.xml");
Student student = (Student) unmarshaller.unmarshal(xmlFile);
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Class: " + student.getClassName());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import java.io.File;
public class ObjectToXMLConverter {
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Student student = new Student();
student.setAge(18);
File xmlFile = new File("student.xml");
marshaller.marshal(student, xmlFile);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}