@Root public class Book { @Element private String title; @Element private String author; // getters and setters } public class Main { public static void main(String[] args) { Book book = new Book(); book.setTitle("Java Programming"); book.setAuthor("John Doe"); Serializer serializer = new Persister(); StringWriter writer = new StringWriter(); try { serializer.write(book, writer); } catch (Exception e) { e.printStackTrace(); } String xml = writer.toString(); System.out.println(xml); StringReader reader = new StringReader(xml); try { Book deserializedBook = serializer.read(Book.class, reader); System.out.println(deserializedBook.getTitle()); System.out.println(deserializedBook.getAuthor()); } catch (Exception e) { e.printStackTrace(); } } }


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