package j2se.core.base;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Lenovo on 2015/11/26.
*/
public class XMLUtil {
/**
* 读取由objSource指定的XML文件中的序列化保存的对象,返回的结果经过了List封装
*
* @param objSource
* 带全部文件路径的文件全名
* @return 由XML文件里面保存的对象构成的List列表(可能是一个或者多个的序列化保存的对象)
* @throws java.io.FileNotFoundException
* 指定的对象读取资源不存在
* @throws java.io.IOException
* 读取发生错误
* @throws Exception
* 其他运行时异常发生
*/
public static List objectXmlDecoder(String objSource) throws FileNotFoundException, IOException, Exception {
List objList = new ArrayList();
File fin = new File(objSource);
FileInputStream fis = new FileInputStream(fin);
XMLDecoder decoder = new XMLDecoder(fis);
Object obj = null;
try {
while ((obj = decoder.readObject()) != null) {
objList.add(obj);
}
} catch (Exception e) {
}
fis.close();
decoder.close();
return objList;
}
/**
* 保存一个集合到XML文件
* @param objects
* @param fileName
* @throws IOException
*/
public static void objectXmlEncoderList(List objects,String fileName) throws IOException {
// 创建输出文件
File fo = new File(fileName);
// 文件不存在,就创建该文件
if (!fo.exists()) {
// 先创建文件的目录
String path = fileName.substring(0, fileName.lastIndexOf('.'));
File pFile = new File(path);
pFile.mkdirs();
}
// 创建文件输出流
FileOutputStream fos = new FileOutputStream(fo);
// 创建XML文件对象输出类实例
XMLEncoder encoder = new XMLEncoder(fos);
// 对象序列化输出到XML文件
for (Object obj : objects) {
encoder.writeObject(obj);
}
encoder.flush();
// 关闭序列化工具
encoder.close();
// 关闭输出流
fos.close();
}
/**
* 把java的可序列化的对象(实现Serializable接口)序列化保存到XML文件里面,如果想一次保存多个可序列化对象请用集合进行封装 保存时将会用现在的对象原来的XML文件内容
*
* @param obj
* 要序列化的可序列化的对象
* @param fileName
* 带完全的保存路径的文件名
* @throws FileNotFoundException
* 指定位置的文件不存在
* @throws IOException
* 输出时发生异常
* @throws Exception
* 其他运行时异常
*/
public static void objectXmlEncoder(Object obj, String fileName) throws FileNotFoundException, IOException, Exception {
// 创建输出文件
File fo = new File(fileName);
// 文件不存在,就创建该文件
if (!fo.exists()) {
// 先创建文件的目录
String path = fileName.substring(0, fileName.lastIndexOf('.'));
File pFile = new File(path);
pFile.mkdirs();
}
// 创建文件输出流
FileOutputStream fos = new FileOutputStream(fo);
// 创建XML文件对象输出类实例
XMLEncoder encoder = new XMLEncoder(fos);
// 对象序列化输出到XML文件
encoder.writeObject(obj);
encoder.flush();
// 关闭序列化工具
encoder.close();
// 关闭输出流
fos.close();
}
}
测试:package j2se.core.base;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Lenovo on 2015/11/26.
*/
public class XmlEncoderTest {
public static void main(String[] args) {
// 使用XMLEncoder将java序列化对象保存到XML文件
String file = new String("d:/test/person.xml");
List personList = new ArrayList(2);
// 构造一个Person对象
Person person = new Person();
person.setId(100001);
person.setName("张三");
person.setPhoneNum("15825625152");
// animal1
Animal animal1 = new Animal();
animal1.setId(1);
animal1.setName("petter");
// animal2
Animal animal2 = new Animal();
animal2.setId(2);
animal2.setName("大黄");
List animals = new ArrayList<>(2);
animals.add(animal1);
animals.add(animal2);
person.setAnimals(animals);
// Person2
Person person2 = new Person();
person2.setId(100002);
person2.setName("历史");
person2.setPhoneNum("158525252551");
List animalList = new ArrayList<>(1);
Animal animal = new Animal();
animal.setId(3);
animal.setName("果果");
animalList.add(animal);
person2.setAnimals(animalList);
personList.add(person);
personList.add(person2);
try {
XMLUtil.objectXmlEncoderList(personList, file);
List persons = XMLUtil.objectXmlDecoder(file);
for (Person p : persons) {
System.out.println(p);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成的XML文件:<?xml version="1.0" encoding="UTF-8"?>
1
petter
2
大黄
100001
张三
15825625152
3
果果
100002
历史
158525252551