import jakarta.xml.bind.annotation.*; @XmlRootElement public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @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; } } import jakarta.xml.bind.*; public class Main { public static void main(String[] args) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, System.out); } } <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <person> <age>25</age> </person>


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