码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA序列化和反序列化XML

时间:2014-06-22 18:30:25      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:java

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:[篮球, 足球]}







JAVA序列化和反序列化XML,布布扣,bubuko.com

JAVA序列化和反序列化XML

标签:java

原文地址:http://blog.csdn.net/kevin_luan/article/details/32313409

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!