使用XStream是实现XML与Java对象的转换(6)--持久化
package cn.tjpu.zhw.xml.xstream6; import java.io.File;import java.util.List; import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;import com.thoughtworks.xstream.persistence.PersistenceStrategy;import com.thoughtworks.xstream.persistence.XmlArrayList; public class PersistenceMain { public static void main(String[] args) { //创建持久化策略(定义存储工具和存储位置) //注:d:/tmp是一个已存在的目录,否则会报错 PersistenceStrategy strategy = new FilePersistenceStrategy( new File("d:/tmp")); //创建操作工具 List list = new XmlArrayList(strategy); System.out.println("刚创建XmlArrayList对象时list.size()="+list.size()); //添加数据 list.add(new Person("张三")); list.add(new Person("李四")); list.add(new Person("毛毛")); list.add(new Person("大熊")); System.out.println("添加了4个元素之后list.size()="+list.size()); //删除“李四” list.remove(1); System.out.println("删除了1个元素之后list.size()="+list.size()); } } class Person { public Person(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Person对象的name=" + getName(); }}
刚创建XmlArrayList对象时list.size()=0添加了4个元素之后list.size()=4删除了1个元素之后list.size()=3
<cn.tjpu.zhw.xml.xstream6.Person> <name>张三</name></cn.tjpu.zhw.xml.xstream6.Person>
<cn.tjpu.zhw.xml.xstream6.Person> <name>毛毛</name></cn.tjpu.zhw.xml.xstream6.Person>
<cn.tjpu.zhw.xml.xstream6.Person> <name>大熊</name></cn.tjpu.zhw.xml.xstream6.Person>
package cn.tjpu.zhw.xml.xstream6; import java.io.File;import java.util.Iterator;import java.util.List; import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;import com.thoughtworks.xstream.persistence.PersistenceStrategy;import com.thoughtworks.xstream.persistence.XmlArrayList; public class UsageTestMain { // XmlArrayList的用法 public static void main(String[] args) { // 创建持久化策略(定义存储工具和存储位置) // 注:d:/tmp是一个已存在的目录,否则会报错 PersistenceStrategy strategy = new FilePersistenceStrategy(new File( "d:/tmp")); // 创建操作工具 List list = new XmlArrayList(strategy); System.out.println("刚创建XmlArrayList对象时list.size()="+list.size()); System.out.println(); //获取迭代器 Iterator iter = list.iterator(); System.out.println("******遍历每一个元素******"); //遍历每一个元素 while(iter.hasNext()){ Person p = (Person)iter.next(); System.out.println("当前元素p="+p); } } }
刚创建XmlArrayList对象时list.size()=3 ******遍历每一个元素******当前元素p=Person对象的name=张三当前元素p=Person对象的name=毛毛当前元素p=Person对象的name=大熊
package cn.tjpu.zhw.xml.xstream6; import java.io.File;import java.util.ArrayList;import java.util.Collection; import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.converters.Converter;import com.thoughtworks.xstream.converters.MarshallingContext;import com.thoughtworks.xstream.converters.UnmarshallingContext;import com.thoughtworks.xstream.io.HierarchicalStreamReader;import com.thoughtworks.xstream.io.HierarchicalStreamWriter;import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;import com.thoughtworks.xstream.persistence.XmlArrayList; //自己定义一个局部转换器public final class LocalArrayListConverter implements Converter { private XStream xstream; public LocalArrayListConverter(XStream xstream) { this.xstream = xstream; } //将Collection对象序列化到文件中 //注:序列化时内存中并没有存放集合中的内容,只是暂存了这些文件存放的目录 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { File dir = new File("d:/tmp"); //创建持久化工具,并加载目录中的所有文件 XmlArrayList list = new XmlArrayList( new FilePersistenceStrategy(dir,xstream)); context.convertAnother(dir); //生成文件 list.addAll((Collection) source); } //从文件中读取信息,反序列换为Collection对象 //注:反序列化时会删除暂存目录下的所有文件 public Object unmarshal( HierarchicalStreamReader reader, UnmarshallingContext context) { File directory = (File) context.convertAnother(null, File.class); //创建持久化工具,并加载目录中的所有文件 XmlArrayList persistentList = new XmlArrayList( new FilePersistenceStrategy(directory, xstream)); //将已加载的信息复制一份到list中 ArrayList list = new ArrayList(persistentList); //删除所有文件 persistentList.clear(); //返回已加载的信息 return list; } public boolean canConvert(Class type) { return type == ArrayList.class; }}
package cn.tjpu.zhw.xml.xstream6; import java.util.ArrayList;import java.util.Collection;import java.util.List; import com.thoughtworks.xstream.XStream; public class LocalConverterMain { public static void main(String[] args) { XStream xstream = new XStream(); xstream.alias("volume", Volume.class); //使用自定义的转换器LocalArrayListConverter来转换Volume类的documents字段 //这个转换器是受限制的局部(local)转换器,只能转换Volume类的documents字段 xstream.registerLocalConverter(Volume.class, "documents", new LocalArrayListConverter(xstream)); //要转换的对象 Volume volume = new Volume(); //创建集合 Collection coll = new ArrayList(); coll.add(1); coll.add(2.123d); coll.add(new Person("张三")); volume.documents.addAll(coll); System.out.println("******序列化******"); //转换XML String xml = xstream.toXML(volume); //输出XML System.out.println(xml); System.out.println("******反序列化******"); Volume v2 = (Volume)xstream.fromXML(xml); for(Object obj:v2.documents){ System.out.println("obj="+obj); } } } abstract class AbstractDocument { String title;} class TextDocument extends AbstractDocument { List chapters = new ArrayList();} class ScannedDocument { List images = new ArrayList();} class Volume { List documents = new ArrayList();}
******序列化******<volume> <documents>d:\tmp</documents></volume>******反序列化******obj=Person对象的name=张三obj=Person对象的name=毛毛obj=Person对象的name=大熊obj=1obj=2.123obj=Person对象的name=张三
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?