我嘗試著用它來保存繼承了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 有很詳細的文檔介紹。