<employee> <name>John Doe</name> <age>30</age> <department>IT</department> </employee> @XmlRootElement public class Employee { private String name; private int age; private String department; @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public int getAge() { return age; } public void setAge(int age) { this.age = age; } @XmlElement public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } } import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class Main { public static void main(String[] args) { try { File file = new File("employee.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(file); System.out.println(employee.getName()); System.out.println(employee.getAge()); System.out.println(employee.getDepartment()); } catch (JAXBException e) { e.printStackTrace(); } } }


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