@XmlRootElement public class Student { private String name; private int age; private double score; // getter and setter methods for the attributes // additional methods if needed } JAXBContext context = JAXBContext.newInstance(Student.class); Student student = new Student(); student.setAge(20); student.setScore(90.0); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(student, new File("student.xml")); UnMarshaller unMarshaller = context.createUnMarshaller(); File file = new File("student.xml"); Student student = (Student) unMarshaller.unmarshal(file); <dependencies> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>3.0.1</version> </dependency> </dependencies>


上一篇:
下一篇:
切换中文