@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
package org.sl.bean;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class ObjectXmlSerial {
public static void main(String[] args) throws IOException{
UserBean user = new UserBean();
OtherUserInfoBean otherUserInfo = new OtherUserInfoBean();
otherUserInfo.setAddress("汉字");
otherUserInfo.setEmail("test@test.com");
user.setName("hello");
user.setPassword("world");
user.setOtherUserInfo(otherUserInfo);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedOutputStream bufferOut = new BufferedOutputStream(byteArrayOut);
writeObjectToXML(bufferOut, user);
byte[] bys = byteArrayOut.toByteArray();
byteArrayOut.close();
bufferOut.close();
ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(bys);
BufferedInputStream bufferIn = new BufferedInputStream(byteArrayIn);
UserBean user1 = readObjectFromXML(bufferIn);
byteArrayIn.close();
bufferIn.close();
System.out.println(user1.getName());
System.out.println(user1.getOtherUserInfo().getAddress());
}
public static <T extends Serializable> void writeObjectToXML(OutputStream out, T obj){
XMLEncoder xmlEncoder = null;
try{
xmlEncoder = new XMLEncoder(out);