import jakarta.xml.bind.*; import java.io.File; public class JAXBExample { public static void main(String[] args) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Employee employee = new Employee(); employee.setId(1); employee.setName("John Doe"); employee.setDepartment("IT"); marshaller.marshal(employee, new File("employee.xml")); } catch (JAXBException e) { e.printStackTrace(); } } } @XmlRootElement class Employee { private int id; private String name; private String department; @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 String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } }


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