其一,如果只是想利用xml来作为中间转换媒介,那很简单:
比如类A需要serialize
那就 implements Serializable {
...
public void save (OutputStream os)
...
public BugReport restore (InputStream is)
...
}然后,生成XML时,把OutputStream编码后放在某一个tag之间,比如<object>Deserialize时,首先利用JDOM API 读入<object>的内容,解码,然后调用restore()就行了。其二,如果不是上述,我曾经看到过一个东东:Jato.
http://www.krumel.com/jato/
希望有所帮助。
Jato is an open-sourced, XML based non-procedural language for transforming any XML document to/from any set of Java objects.
比如类A需要serialize
那就 implements Serializable {
...
public void save (OutputStream os)
...
public BugReport restore (InputStream is)
...
}然后,生成XML时,把OutputStream编码后放在某一个tag之间,比如<object>Deserialize时,首先利用JDOM API 读入<object>的内容,解码,然后调用restore()就行了。其二,如果不是上述,我曾经看到过一个东东:Jato.
http://www.krumel.com/jato/
希望有所帮助。
Jato is an open-sourced, XML based non-procedural language for transforming any XML document to/from any set of Java objects.
import java.io.*; public class Queue extends Vector {
/*
** FIFO
*/
Queue() {
super();
}
void put(Object o) {
addElement(o);
} Object get() {
if (isEmpty()) return null;
Object o = firstElement();
removeElement(o);
return o;
} Object peek() {
if (isEmpty()) return null;
return firstElement();
}
public static void main(String args[]) {
Queue theQueue;
theQueue = new Queue();
theQueue.put("element 1");
theQueue.put("element 2");
theQueue.put("element 3");
theQueue.put("element 4");
System.out.println(theQueue.toString());
// serialize the Queue
System.out.println("serializing theQueue");
try {
FileOutputStream fout = new FileOutputStream("thequeue.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(theQueue);
oos.close();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void main_load(String args[]) {
Queue theQueue;
theQueue = new Queue();
// unserialize the Queue
System.out.println("unserializing theQueue");
try {
FileInputStream fin = new FileInputStream("thequeue.dat");
ObjectInputStream ois = new ObjectInputStream(fin);
theQueue = (Queue) ois.readObject();
ois.close();
}
catch (Exception e) { e.printStackTrace(); }
System.out.println(theQueue.toString());
}
}
XML serialization.上面的老兄说的是第一部分,例如处理图形等.Thank you anyway.