package com.lss.utils; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * JAVA序列化和反序列化XML * <p> * 属性是包含getter,setter属性生成, 序列化的类必须包含一个公共无参构造函数。 * </p> * * @author kevin LUAN * */ public class SerializableXmlUtils { @SuppressWarnings("unchecked") public static <T> T parserXML(String xml) { ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes()); XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(in)); decoder.close(); return (T) decoder.readObject(); } public static <T> String formatXML(T entity) { ByteArrayOutputStream out = new ByteArrayOutputStream(); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(out)); encoder.writeObject(entity); encoder.close(); return out.toString(); } public static void main(String[] args) throws Exception { User user = new User(); user.setId(1); user.setName("张三丰"); user.setAihao(new String[] { "篮球", "足球" }); user.setStudent(new Student(1, "二年级")); String xml = formatXML(user); System.out.println("序列化到XML:\n" + xml); user = parserXML(xml); System.out.println("反序列化Bean:\n" + user); } }# 测试Bean Code
package com.lss.utils; import java.util.Arrays; public class User { private int id; private String name; private Student student; private String aihao[]; @Override public String toString() { return "{id:" + id + ",name:" + name + ",student:" + student + ",aihao:" + Arrays.toString(aihao) + "}"; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getAihao() { return aihao; } public void setAihao(String[] aihao) { this.aihao = aihao; } }
package com.lss.utils; public class Student { private int classId; private String level; public Student() { } public Student(int classId, String level) { this.classId = classId; this.level = level; } public int getClassId() { return classId; } public void setClassId(int classId) { this.classId = classId; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } @Override public String toString() { return "{classId:" + classId + ",level:" + level + "}"; } }
序列化到XML:
<?xml version="1.0" encoding="UTF-8"?> <java version="1.7.0_45" class="java.beans.XMLDecoder"> <object class="com.koudai.laohuji.service.User"> <void property="aihao"> <array class="java.lang.String" length="2"> <void index="0"> <string>篮球</string> </void> <void index="1"> <string>足球</string> </void> </array> </void> <void property="id"> <int>1</int> </void> <void property="name"> <string>张三丰</string> </void> <void property="student"> <object class="com.koudai.laohuji.service.Student"> <void property="classId"> <int>1</int> </void> <void property="level"> <string>二年级</string> </void> </object> </void> </object> </java>
反序列化Bean:
{id:1,name:张三丰,student:{classId:1,level:二年级},aihao:[篮球, 足球]}