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; } } JAXBContext context = JAXBContext.newInstance(Book.class); Book book = new Book(); book.setTitle("Java Programming"); book.setAuthor("John Smith"); Marshaller marshaller = context.createMarshaller(); <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <book> <title>Java Programming</title> <author>John Smith</author> </book> String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> " + "<book> " + " <title>Java Programming</title> " + " <author>John Smith</author> " + "</book>"; Unmarshaller unmarshaller = context.createUnmarshaller(); System.out.println("Title: " + book.getTitle()); System.out.println("Author: " + book.getAuthor()); Title: Java Programming Author: John Smith


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