@XmlRootElement(name = "Student") public class Student { @XmlElement(name = "Name") private String name; @XmlElement(name = "Age") private int age; } xjc -d src/main/java -p com.example.model schema.xsd JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Student student = (Student) unmarshaller.unmarshal(new File("student.xml")); System.out.println("Name: " + student.getName()); System.out.println("Age: " + student.getAge()); JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Student student = new Student(); student.setAge(20); marshaller.marshal(student, new File("student.xml")); <dependencies> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.3</version> </dependency> </dependencies>


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