import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class Person { @Element private String name; @Element private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } // getters and setters // ... } import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import java.io.File; public class SerializationExample { public static void main(String[] args) throws Exception { Person person = new Person("John Doe", 30); Serializer serializer = new Persister(); File file = new File("person.xml"); serializer.write(person, file); } } import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import java.io.File; public class DeserializationExample { public static void main(String[] args) throws Exception { File file = new File("person.xml"); Serializer serializer = new Persister(); Person person = serializer.read(Person.class, file); System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } }


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