<book> <title>Java Programming</title> <author>John Smith</author> </book> @XmlRootElement public class Book { @XmlElement private String title; @XmlElement private String author; } JAXBContext context = JAXBContext.newInstance(Book.class); Marshaller marshaller = context.createMarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller(); Book book = new Book("Java Programming", "John Smith"); marshaller.marshal(book, new File("book.xml")); Book parsedBook = (Book) unmarshaller.unmarshal(new File("book.xml")); @XmlRootElement(namespace = "http://example.com/books") public class Book { @XmlElement(namespace = "http://example.com/books") private String title; @XmlElement(namespace = "http://example.com/books") private String author; } @XmlRootElement public class Book { @XmlAttribute private String id; @XmlElement private String title; @XmlElement private String author; } @XmlRootElement public class Library { @XmlElementWrapper(name = "books") @XmlElement(name = "book") private List<Book> bookList; } @XmlRootElement public class Library { @XmlElements({ @XmlElement(name = "book", type = Book.class), @XmlElement(name = "magazine", type = Magazine.class) }) private List<Object> items; } public class Magazine { @XmlElement private String name; }


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