今天需要把数据库的数据导出l,然后也可以从外面导入保存到数据库。
考虑导出的数据格式为xml或json。json的话可以用google的gson实现。
以前做过。导出为xml的话,以前都是用java拼装或jdom或dom4j。今天
发现xstream也很强大,既可以把java对象转化为xml,也可以从xml转化为java
对象。专业说法,就是可以序列化为xml,也可以凡序列化为java对象。当然xml也完美支持
json的序列化和反序列化,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的
相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar
以下是自己写的模拟例子。jar和代码在附件中。
需要的jar为xstream-1.3.1.jar(必须的),xpp3_min-1.1.4c.jar(可选的)
Test.java 把java对象转化为xml
Java代码
importjava.util.ArrayList;
importjava.util.List;
importcom.thoughtworks.xstream.XStream;
publicclassTest
{
/*
* java object to xml
*/
privatestaticXmlBean xmlBean;
publicstaticvoidmain(String[] args)
{
//instantiate the XStream class
XStream xstream =newXStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
setData();
String xml = xstream.toXML(xmlBean);
System.out.println(xml);
}
//初始化数据
publicstaticvoidsetData()
{
List stepList =newArrayList();
Step s;
for(inti=0;i<5;i++)
{
s=newStep();
s.setId(newLong(i));
s.setSeq(newLong(i+i));
s.setName("step"+i);
stepList.add(s);
}
Action a;
List actionList =newArrayList();
for(inti=0;i<5;i++)
{
a=newAction();
a.setId(newLong(i));
a.setIsSub(i+i);
a.setName("action"+i);
actionList.add(a);
}
Flow flow =newFlow();
flow.setActionId(1L);
flow.setClassId(3L);
flow.setSclassId(3L);
flow.setName("flow_analy");
flow.setStepId(4L);
flow.setActionId(5L);
xmlBean =newXmlBean();
xmlBean.setFlow(flow);
xmlBean.setStepList(stepList);
xmlBean.setActionList(actionList);
}
}import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
public class Test
{
/*
* java object to xml
*/
private static XmlBean xmlBean;
public static void main(String[] args)
{
//instantiate the XStream class
XStream xstream = new XStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
setData();
String xml = xstream.toXML(xmlBean);
System.out.println(xml);
}
//初始化数据
public static void setData()
{
List stepList = new ArrayList();
Step s;
for(int i=0;i<5;i++)
{
s=new Step();
s.setId(new Long(i));
s.setSeq(new Long(i+i));
s.setName("step"+i);
stepList.add(s);
}
Action a;
List actionList = new ArrayList();
for(int i=0;i<5;i++)
{
a=new Action();
a.setId(new Long(i));
a.setIsSub(i+i);
a.setName("action"+i);
actionList.add(a);
}
Flow flow = new Flow();
flow.setActionId(1L);
flow.setClassId(3L);
flow.setSclassId(3L);
flow.setName("flow_analy");
flow.setStepId(4L);
flow.setActionId(5L);
xmlBean = new XmlBean();
xmlBean.setFlow(flow);
xmlBean.setStepList(stepList);
xmlBean.setActionList(actionList);
}
}
Test1.java 把java对象转换为xml写到文件中
Java代码
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.OutputStream;
importjava.util.ArrayList;
importjava.util.List;
importcom.thoughtworks.xstream.XStream;
publicclassTest2
{
/*
* java object to xml
*/
privatestaticXmlBean xmlBean;
publicstaticvoidmain(String[] args)throwsException
{
//instantiate the XStream class
XStream xstream =newXStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
setData();
OutputStream out =newFileOutputStream("c:/test.xml");
xstream.toXML(xmlBean, out);
out.close();
}
//初始化数据
publicstaticvoidsetData()
{
List stepList =newArrayList();
Step s;
for(inti=0;i<5;i++)
{
s=newStep();
s.setId(newLong(i));
s.setSeq(newLong(i+i));
s.setName("步骤"+i);
stepList.add(s);
}
Action a;
List actionList =newArrayList();
for(inti=0;i<5;i++)
{
a=newAction();
a.setId(newLong(i));
a.setIsSub(i+i);
a.setName("action"+i);
actionList.add(a);
}
Flow flow =newFlow();
flow.setActionId(1L);
flow.setClassId(3L);
flow.setSclassId(3L);
flow.setName("flow_analy");
flow.setStepId(4L);
flow.setActionId(5L);
xmlBean =newXmlBean();
xmlBean.setFlow(flow);
xmlBean.setStepList(stepList);
xmlBean.setActionList(actionList);
}
}import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
public class Test2
{
/*
* java object to xml
*/
private static XmlBean xmlBean;
public static void main(String[] args) throws Exception
{
//instantiate the XStream class
XStream xstream = new XStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
setData();
OutputStream out = new FileOutputStream("c:/test.xml");
xstream.toXML(xmlBean, out);
out.close();
}
//初始化数据
public static void setData()
{
List stepList = new ArrayList();
Step s;
for(int i=0;i<5;i++)
{
s=new Step();
s.setId(new Long(i));
s.setSeq(new Long(i+i));
s.setName("步骤"+i);
stepList.add(s);
}
Action a;
List actionList = new ArrayList();
for(int i=0;i<5;i++)
{
a=new Action();
a.setId(new Long(i));
a.setIsSub(i+i);
a.setName("action"+i);
actionList.add(a);
}
Flow flow = new Flow();
flow.setActionId(1L);
flow.setClassId(3L);
flow.setSclassId(3L);
flow.setName("flow_analy");
flow.setStepId(4L);
flow.setActionId(5L);
xmlBean = new XmlBean();
xmlBean.setFlow(flow);
xmlBean.setStepList(stepList);
xmlBean.setActionList(actionList);
}
}
Test3.java 把硬盘上的文件读取并转化为java对象。
Java代码
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.util.List;
importcom.thoughtworks.xstream.XStream;
importcom.thoughtworks.xstream.io.xml.DomDriver;
publicclassTest3
{
/**
* java object to xml
*/
privatestaticXmlBean xmlBean;
publicstaticvoidmain(String[] args)throwsException
{
//instantiate the XStream class
XStream xstream =newXStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
String xml = readFile("c:/test.xml");
System.out.println(xml);
//解析的时候一定要 Specify another driver. For example: new XStream(new DomDriver())
XmlBean bean = (XmlBean)xstream.fromXML(xml);
System.out.println(bean);
Flow flow = bean.getFlow();
List steps = bean.getStepList();
for(Step step : steps )
{
}
List actions = bean.getActionList();
}
/**
* 读取文件内容
* @param fileName
* @return
* @throws Exception
*/
publicstaticString readFile(String fileName)throwsException {
String fileContent ="";
File f =newFile(fileName);
FileReader fileReader =newFileReader(f);
BufferedReader reader =newBufferedReader(fileReader);
String line ="";
while((line = reader.readLine()) !=null)
{
fileContent = fileContent + line;
}
reader.close();
returnfileContent;
}
}import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Test3
{
/**
* java object to xml
*/
private static XmlBean xmlBean;
public static void main(String[] args) throws Exception
{
//instantiate the XStream class
XStream xstream = new XStream();
xstream.alias("step", Step.class);
xstream.alias("action", Action.class);
xstream.alias("flow", Flow.class);
//Serializing an object to XML
String xml = readFile("c:/test.xml");
System.out.println(xml);
//解析的时候一定要 Specify another driver. For example: new XStream(new DomDriver())
XmlBean bean = (XmlBean)xstream.fromXML(xml);
System.out.println(bean);
Flow flow = bean.getFlow();
List steps = bean.getStepList();
for(Step step : steps )
{
}
List actions = bean.getActionList();
}
/**
* 读取文件内容
* @param fileName
* @return
* @throws Exception
*/
public static String readFile(String fileName) throws Exception {
String fileContent = "";
File f = new File(fileName);
FileReader fileReader = new FileReader(f);
BufferedReader reader = new BufferedReader(fileReader);
String line = "";
while ((line = reader.readLine()) != null)
{
fileContent = fileContent + line;
}
reader.close();
return fileContent;
}
}
下面三个javabean,XStream不限制你的属性的可见性,默认所有属性都会进行转换; XStream不要求你必须要有 setter和 getter方法,也不要求你要有一个默认的类构造方法。
Step.java
Java代码
publicclassStep
{
privateLong id;
privateString name;
privateLong seq;
privateString remarks;
publicLong getId()
{
returnid;
}
publicvoidsetId(Long id)
{
this.id = id;
}
publicString getName()
{
returnname;
}
publicvoidsetName(String name)
{
this.name = name;
}
publicLong getSeq()
{
returnseq;
}
publicvoidsetSeq(Long seq)
{
this.seq = seq;
}
publicString getRemarks()
{
returnremarks;
}
publicvoidsetRemarks(String remarks)
{
this.remarks = remarks;
}
}public class Step
{
private Long id;
private String name;
private Long seq;
private String remarks;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Long getSeq()
{
return seq;
}
public void setSeq(Long seq)
{
this.seq = seq;
}
public String getRemarks()
{
return remarks;
}
public void setRemarks(String remarks)
{
this.remarks = remarks;
}
}
Action.java
Java代码
publicclassAction
{
privateLong id;
privateString name;
privateintisSub;
privateString remarks;
publicLong getId()
{
returnid;
}
publicvoidsetId(Long id)
{
this.id = id;
}
publicString getName()
{
returnname;
}
publicvoidsetName(String name)
{
this.name = name;
}
publicintgetIsSub()
{
returnisSub;
}
publicvoidsetIsSub(intisSub)
{
this.isSub = isSub;
}
publicString getRemarks()
{
returnremarks;
}
publicvoidsetRemarks(String remarks)
{
this.remarks = remarks;
}
}public class Action
{
private Long id;
private String name;
private int isSub;
private String remarks;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getIsSub()
{
return isSub;
}
public void setIsSub(int isSub)
{
this.isSub = isSub;
}
public String getRemarks()
{
return remarks;
}
public void setRemarks(String remarks)
{
this.remarks = remarks;
}
}
Flow.java
Java代码
publicclassFlow
{
privateLong id;
privateString name;
privateLong classId;
privateLong sclassId;
privateLong stepId;
privateLong actionId;
publicLong getId()
{
returnid;
}
publicvoidsetId(Long id)
{
this.id = id;
}
publicString getName()
{
returnname;
}
publicvoidsetName(String name)
{
this.name = name;
}
publicLong getClassId()
{
returnclassId;
}
publicvoidsetClassId(Long classId)
{
this.classId = classId;
}
publicLong getSclassId()
{
returnsclassId;
}
publicvoidsetSclassId(Long sclassId)
{
this.sclassId = sclassId;
}
publicLong getStepId()
{
returnstepId;
}
publicvoidsetStepId(Long stepId)
{
this.stepId = stepId;
}
publicLong getActionId()
{
returnactionId;
}
publicvoidsetActionId(Long actionId)
{
this.actionId = actionId;
}
}
posted on 2012-05-14 16:01 透明的鱼 阅读(3861) 评论(0) 编辑 收藏