xstream 让javabean和xml互相转换(转载)
今天需要把数据库的数据导出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
- 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<Step> stepList = new ArrayList<Step>();
- 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<Action> actionList = new ArrayList<Action>();
- 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);
- }
- }
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<Step> stepList = new ArrayList<Step>(); 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<Action> actionList = new ArrayList<Action>(); 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写到文件中
- 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<Step> stepList = new ArrayList<Step>();
- 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<Action> actionList = new ArrayList<Action>();
- 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);
- }
- }
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<Step> stepList = new ArrayList<Step>(); 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<Action> actionList = new ArrayList<Action>(); 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对象。
- 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<Step> steps = bean.getStepList();
- for(Step step : steps )
- {
- }
- List<Action> 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;
- }
- }
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<Step> steps = bean.getStepList(); for(Step step : steps ) { } List<Action> 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
- 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;
- }
- }
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
- 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;
- }
- }
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
- public class Flow
- {
- private Long id;
- private String name;
- private Long classId;
- private Long sclassId;
- private Long stepId;
- private Long actionId;
- 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 getClassId()
- {
- return classId;
- }
- public void setClassId(Long classId)
- {
- this.classId = classId;
- }
- public Long getSclassId()
- {
- return sclassId;
- }
- public void setSclassId(Long sclassId)
- {
- this.sclassId = sclassId;
- }
- public Long getStepId()
- {
- return stepId;
- }
- public void setStepId(Long stepId)
- {
- this.stepId = stepId;
- }
- public Long getActionId()
- {
- return actionId;
- }
- public void setActionId(Long actionId)
- {
- this.actionId = actionId;
- }
- }