<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>3.0.1</version> </dependency> import jakarta.xml.bind.annotation.*; @XmlRootElement public class Book { private String title; private String author; public String getTitle() { return title; } @XmlElement public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } @XmlElement public void setAuthor(String author) { this.author = author; } } import jakarta.xml.bind.*; public class JAXBExample { public static void main(String[] args) throws JAXBException { Book book = new Book(); book.setTitle("Java Programming"); book.setAuthor("John Doe"); JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(book, System.out); } } <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <book> <title>Java Programming</title> <author>John Doe</author> </book>


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