<employee> <id>1</id> <name>John Doe</name> <salary>5000</salary> </employee> @XmlRootElement(name = "employee") public class Employee { private int id; private String name; private int salary; @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } } public class Main { public static void main(String[] args) throws JAXBException { File file = new File("employee.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) unmarshaller.unmarshal(file); } } @XmlRootElement(name = "employee") public class Employee { private int id; private String name; private int salary; @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } } public class Main { public static void main(String[] args) throws JAXBException { Employee employee = new Employee(); employee.setId(1); employee.setName("John Doe"); employee.setSalary(5000); File file = new File("employee.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(employee, file); } }


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