9910

单飞

   :: 首页 :: 联系 :: 聚合  :: 管理
我尝试着用它来保存继承了Externalizeable的java类,可是没有成功。
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Person implements Externalizable{
    private String name ;
    private int sex;
    private Person son;
    
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public Person getSon() {
        return son;
    }
    public void setSon(Person son) {
        this.son = son;
    }
    public void writeExternal(ObjectOutput out) throws IOException{
        out.writeObject(name);
        out.writeInt(sex);
        out.writeObject(son);
        
        
    }
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException{
        name = (String)in.readObject();
        sex = in.readInt();
        son = (Person)in.readObject();
    }
}


import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TestXml {
    public static void main(String[] args) {
        try {
            XStream xstream = new XStream(new DomDriver());
            File file = new File("c:/ssx.xml");
            Writer writer = new FileWriter(file);
            ObjectOutputStream out = xstream.createObjectOutputStream(writer);
            Person father = new Person();
            father.setName("father");
            father.setSex(1);
            Person son = new Person();
            son.setName("son");
            father.setSon(son);
            //----------------write----------
            xstream.alias("Person", Person.class);
            out.writeObject(father);
            out.close();
            //-----------------read--------------
            Reader reader = new FileReader(file);
            ObjectInputStream in = xstream.createObjectInputStream(reader);
            Person sfather = (Person)in.readObject();
            System.out.println(sfather.getName());
           
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
可是XStream提供了Converter这个类
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;

public class PersonConverter implements Converter {

public boolean canConvert(Class clazz) {
return false;
}

public void marshal(Object value, HierarchicalStreamWriter writer,
MarshallingContext context) {
}

public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
return null;
}
这样就可以来自己建立Converter如:date..等类型。
然后注册一下:
xStream.registerConverter(new PersonConverter());

通过对比发现XStream要比betwixt更易于使用,betwixt需要更多的设置才能运行,并且有大量隐藏的要求。
http://xstream.codehaus.org 有很详细的文档介绍。
posted on 2006-11-26 15:27 单飞 阅读(786) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: