做对象的序列化,到Xml的读写解决方法: 利用开源工具xstream进行对象的序列化到xml的读写,下面的项目主要是:针对一个含有图片的对象的到xml的读写操作。
项目结构及所需jar包: 实体类编写:import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("Preparation")
public class Preparation {
@XStreamAsAttribute
public String preparationText;
@XStreamAsAttribute
public String preparationType;
@XStreamAsAttribute
public String questionText;
@XStreamAsAttribute
public String questionAnswer;
@XStreamAsAttribute
public String preparationImage;
@XStreamAsAttribute
public String responseable;
public String getPreparationText() {
return preparationText;
}
public void setPreparationText(String preparationText) {
this.preparationText = preparationText;
}
public String getPreparationType() {
return preparationType;
}
public void setPreparationType(String preparationType) {
this.preparationType = preparationType;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public String getQuestionAnswer() {
return questionAnswer;
}
public void setQuestionAnswer(String questionAnswer) {
this.questionAnswer = questionAnswer;
}
public String getPreparationImage() {
return preparationImage;
}
public void setPreparationImage(String preparationImage) {
this.preparationImage = preparationImage;
}
public String getResponseable() {
return responseable;
}
public void setResponseable(String responseable) {
this.responseable = responseable;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return preparationText+";"+preparationImage;
}
}
图片操作类(将图片转换为字节流进行存储并读取)package cn.mccn.com.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Image {
/*
* 输入流,读取图片或者Txt文件的字节码
*/
@SuppressWarnings("finally")
public static String getRimg(String file1) {
StringBuilder builder = new StringBuilder(1000000);
byte[] bytes = null;
try {
new File(file1).createNewFile();
FileInputStream in = new FileInputStream(file1);
BufferedInputStream bis = new BufferedInputStream(in);
bytes = new byte[bis.available()];
bis.read(bytes);
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return new BASE64Encoder().encodeBuffer(bytes);
}
}
/*
* 输出流,根据输入流传递的字节码生成图片或者txt等文件
*/
public static void getWimg(String content, String file2) {
try {
FileOutputStream out = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(out);
DataOutputStream dos = new DataOutputStream(bos);
byte[] bs = new BASE64Decoder().decodeBuffer(content);
dos.write(bs);
dos.flush();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
xml操作工具类(主要进行xml格式定制,读写xml)package cn.mccn.com.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import cn.mccn.com.bean.*;
public class Util {
/** 定制Xml格式和属性标签*/
private static XStream setXStream(XStream stream) {
// stream.alias("Page", Page.class);
stream.alias("Preparation", Preparation.class);
stream.aliasAttribute(Preparation.class, "preparationText", "preparationText");
stream.aliasAttribute(Preparation.class, "preparationType", "preparationType");
stream.aliasAttribute(Preparation.class, "questionText", "questionText");
stream.aliasAttribute(Preparation.class, "questionAnswer", "questionAnswer");
stream.aliasAttribute(Preparation.class, "responseable", "responseable");
stream.aliasField("preparationImage", Preparation.class, "preparationImage");
return stream;
}
/* * 根据定制创建Xml 文件 */
public static String makeXML(List list) {
XStream stream = new XStream();
stream = setXStream(stream);
System.out.println(stream.toString());
String str = stream.toXML(list);
System.out.println(str.toString());
return str;
}
/* * 将对象根据定制产生的内容写入Xml 文件 */
public static void writeXML(String s) {
FileWriter fw = null;
String str="<?xml version='1.0' encoding='UTF-8'?>"+s;
try {
fw = new FileWriter("e:/testXtram.xml");
fw.write(str, 0, str.length());
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/** xstream方式解析Xml*/
public static List readXML2(InputStream inputStream) {
XStream stream = new XStream(new DomDriver());
stream = setXStream(stream);
return (List) stream.fromXML(inputStream);
}
}
Main方法类(进行读写对象到xml的测试) package cn.mccn.com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import cn.mccn.com.bean.*;
public class Main {
public static void insertXml(List list) {
List preparations=new ArrayList();
for (int i = 0; i < list.size(); i++) {
Preparation preparation=new Preparation();
preparation=(Preparation)list.get(i);
String file1 =preparation.getPreparationImage();
String content = Image.getRimg(file1);
preparation.setPreparationImage(content.toString());
preparations.add(preparation);
}
Util.writeXML(Util.makeXML(preparations));
System.out.println(Util.makeXML(preparations).toString());
}
public static List exportXml(String file,String file2) {
File f2 = new File(file);
List list=new ArrayList();
List list1=new ArrayList();
try {
InputStream in = new FileInputStream(f2);
list = Util.readXML2(in);
for (int i = 0; i < list.size(); i++) {
Preparation preparation = (Preparation) list.get(i);
String content = preparation.getPreparationImage();
Image.getWimg(content.trim(), file2+"1"+i+".jpg");
preparation.setPreparationImage(file2+"1"+i+".jpg");
list1.add(preparation);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list1;
}
public static void main(String[]args){
Preparation preparation=new Preparation();
preparation.setQuestionText("张涛");
preparation.setPreparationType("0");
preparation.setPreparationText("0");
preparation.setPreparationImage("e:/0.jpg");
preparation.setQuestionAnswer("0");
preparation.setResponseable("0");
List list=new ArrayList();
// list.add(preparation);
// insertXml(list);
list= exportXml("e:/testXtram.xml", "f:/");
System.out.println(list.size()+";"+list.get(0).toString());
}
}