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);
- }
- }
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);
- }
- }
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;
- }
- }
下面三個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;
- }
- }
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;
- }
- }
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;
- }
- }