swzhebei

常用链接

统计

最新评论

  • 1. re: 调用百度地图小实例
  • 如果我有100个经纬度 请问,您是不是再代码里写100个?你这样没有价值,如何获取动态的请说明!
  • --toly
  • 2. re: 调用百度地图小实例
  • 更改经纬度就不行了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • --你姥姥

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

 

Java代码 复制代码 收藏代码
  1. import java.util.ArrayList;   
  2. import java.util.List;   
  3.   
  4. import com.thoughtworks.xstream.XStream;   
  5.   
  6.   
  7. public class Test   
  8. {   
  9.     /*  
  10.      * java object to xml  
  11.      */  
  12.        
  13.     private static XmlBean xmlBean;   
  14.     public static void main(String[] args)   
  15.     {   
  16.         //instantiate the XStream class   
  17.         XStream xstream = new XStream();   
  18.         xstream.alias("step", Step.class);   
  19.         xstream.alias("action", Action.class);   
  20.         xstream.alias("flow", Flow.class);   
  21.            
  22.         //Serializing an object to XML   
  23.            
  24.         setData();   
  25.            
  26.         String xml = xstream.toXML(xmlBean);   
  27.         System.out.println(xml);   
  28.     }   
  29.     //初始化数据   
  30.     public static void setData()   
  31.     {   
  32.         List<Step> stepList = new ArrayList<Step>();   
  33.         Step s;   
  34.         for(int i=0;i<5;i++)   
  35.         {   
  36.             s=new Step();   
  37.             s.setId(new Long(i));   
  38.             s.setSeq(new Long(i+i));   
  39.             s.setName("step"+i);   
  40.             stepList.add(s);   
  41.                
  42.         }   
  43.            
  44.         Action a;   
  45.         List<Action> actionList = new ArrayList<Action>();   
  46.         for(int i=0;i<5;i++)   
  47.         {   
  48.             a=new Action();   
  49.             a.setId(new Long(i));   
  50.             a.setIsSub(i+i);   
  51.             a.setName("action"+i);   
  52.             actionList.add(a);   
  53.                
  54.         }   
  55.            
  56.         Flow flow = new Flow();   
  57.         flow.setActionId(1L);   
  58.         flow.setClassId(3L);   
  59.         flow.setSclassId(3L);   
  60.         flow.setName("flow_analy");   
  61.         flow.setStepId(4L);   
  62.         flow.setActionId(5L);   
  63.            
  64.         xmlBean = new XmlBean();   
  65.         xmlBean.setFlow(flow);   
  66.         xmlBean.setStepList(stepList);   
  67.         xmlBean.setActionList(actionList);   
  68.     }   
  69. }  

 Test1.java 把java对象转换为xml写到文件中

 

Java代码 复制代码 收藏代码
  1. import java.io.FileNotFoundException;   
  2. import java.io.FileOutputStream;   
  3. import java.io.OutputStream;   
  4. import java.util.ArrayList;   
  5. import java.util.List;   
  6.   
  7. import com.thoughtworks.xstream.XStream;   
  8.   
  9.   
  10. public class Test2   
  11. {   
  12.     /*  
  13.      * java object to xml  
  14.      */  
  15.        
  16.     private static XmlBean xmlBean;   
  17.     public static void main(String[] args) throws Exception   
  18.     {   
  19.         //instantiate the XStream class   
  20.         XStream xstream = new XStream();   
  21.         xstream.alias("step", Step.class);   
  22.         xstream.alias("action", Action.class);   
  23.         xstream.alias("flow", Flow.class);   
  24.            
  25.         //Serializing an object to XML   
  26.            
  27.         setData();   
  28.            
  29.        
  30.         OutputStream out = new FileOutputStream("c:/test.xml");   
  31.         xstream.toXML(xmlBean, out);   
  32.         out.close();   
  33.     }   
  34.     //初始化数据   
  35.     public static void setData()   
  36.     {   
  37.         List<Step> stepList = new ArrayList<Step>();   
  38.         Step s;   
  39.         for(int i=0;i<5;i++)   
  40.         {   
  41.             s=new Step();   
  42.             s.setId(new Long(i));   
  43.             s.setSeq(new Long(i+i));   
  44.             s.setName("步骤"+i);   
  45.             stepList.add(s);   
  46.                
  47.         }   
  48.            
  49.         Action a;   
  50.         List<Action> actionList = new ArrayList<Action>();   
  51.         for(int i=0;i<5;i++)   
  52.         {   
  53.             a=new Action();   
  54.             a.setId(new Long(i));   
  55.             a.setIsSub(i+i);   
  56.             a.setName("action"+i);   
  57.             actionList.add(a);   
  58.                
  59.         }   
  60.            
  61.         Flow flow = new Flow();   
  62.         flow.setActionId(1L);   
  63.         flow.setClassId(3L);   
  64.         flow.setSclassId(3L);   
  65.         flow.setName("flow_analy");   
  66.         flow.setStepId(4L);   
  67.         flow.setActionId(5L);   
  68.            
  69.         xmlBean = new XmlBean();   
  70.         xmlBean.setFlow(flow);   
  71.         xmlBean.setStepList(stepList);   
  72.         xmlBean.setActionList(actionList);   
  73.     }   
  74. }  

 Test3.java  把硬盘上的文件读取并转化为java对象。

 

Java代码 复制代码 收藏代码
  1. import java.io.BufferedReader;   
  2. import java.io.File;   
  3. import java.io.FileReader;   
  4. import java.util.List;   
  5.   
  6. import com.thoughtworks.xstream.XStream;   
  7. import com.thoughtworks.xstream.io.xml.DomDriver;   
  8.   
  9.   
  10. public class Test3   
  11. {   
  12.     /**  
  13.      * java object to xml  
  14.      */  
  15.        
  16.     private static XmlBean xmlBean;   
  17.     public static void main(String[] args) throws Exception   
  18.     {   
  19.         //instantiate the XStream class   
  20.         XStream xstream = new XStream();   
  21.         xstream.alias("step", Step.class);   
  22.         xstream.alias("action", Action.class);   
  23.         xstream.alias("flow", Flow.class);   
  24.            
  25.         //Serializing an object to XML   
  26.            
  27.            
  28.         String xml =  readFile("c:/test.xml");   
  29.         System.out.println(xml);   
  30.         //解析的时候一定要 Specify another driver. For example: new XStream(new DomDriver())   
  31.         XmlBean bean = (XmlBean)xstream.fromXML(xml);   
  32.         System.out.println(bean);   
  33.         Flow flow = bean.getFlow();   
  34.         List<Step> steps = bean.getStepList();   
  35.         for(Step step : steps )   
  36.         {   
  37.                
  38.         }   
  39.         List<Action> actions = bean.getActionList();   
  40.            
  41.            
  42.     }   
  43.     /**  
  44.      * 读取文件内容  
  45.      * @param fileName  
  46.      * @return  
  47.      * @throws Exception  
  48.      */  
  49.      public static String readFile(String fileName) throws Exception {   
  50.         String fileContent = "";   
  51.         File f = new File(fileName);   
  52.         FileReader fileReader = new FileReader(f);   
  53.         BufferedReader reader = new BufferedReader(fileReader);   
  54.         String line = "";   
  55.         while ((line = reader.readLine()) != null)   
  56.         {   
  57.              fileContent = fileContent + line;   
  58.         }   
  59.         reader.close();   
  60.         return fileContent;   
  61.     }   
  62. }  

 

 

下面三个javabean,XStream 不限制你的属性的可见性,默认所有属性都会进行转换; XStream 不要求你必须要有 setter getter 方法,也不要求你要有一个默认的类构造方法。

Step.java

 

Java代码 复制代码 收藏代码
  1. public class Step   
  2. {   
  3.     private Long id;   
  4.     private String name;   
  5.     private Long seq;   
  6.     private String remarks;   
  7.     public Long getId()   
  8.     {   
  9.         return id;   
  10.     }   
  11.     public void setId(Long id)   
  12.     {   
  13.         this.id = id;   
  14.     }   
  15.     public String getName()   
  16.     {   
  17.         return name;   
  18.     }   
  19.     public void setName(String name)   
  20.     {   
  21.         this.name = name;   
  22.     }   
  23.     public Long getSeq()   
  24.     {   
  25.         return seq;   
  26.     }   
  27.     public void setSeq(Long seq)   
  28.     {   
  29.         this.seq = seq;   
  30.     }   
  31.     public String getRemarks()   
  32.     {   
  33.         return remarks;   
  34.     }   
  35.     public void setRemarks(String remarks)   
  36.     {   
  37.         this.remarks = remarks;   
  38.     }   
  39.        
  40.        
  41.   
  42. }  

 Action.java

 

Java代码 复制代码 收藏代码
  1. public class Action   
  2. {   
  3.     private Long id;   
  4.     private String name;   
  5.     private int isSub;   
  6.     private String remarks;   
  7.     public Long getId()   
  8.     {   
  9.         return id;   
  10.     }   
  11.     public void setId(Long id)   
  12.     {   
  13.         this.id = id;   
  14.     }   
  15.     public String getName()   
  16.     {   
  17.         return name;   
  18.     }   
  19.     public void setName(String name)   
  20.     {   
  21.         this.name = name;   
  22.     }   
  23.     public int getIsSub()   
  24.     {   
  25.         return isSub;   
  26.     }   
  27.     public void setIsSub(int isSub)   
  28.     {   
  29.         this.isSub = isSub;   
  30.     }   
  31.     public String getRemarks()   
  32.     {   
  33.         return remarks;   
  34.     }   
  35.     public void setRemarks(String remarks)   
  36.     {   
  37.         this.remarks = remarks;   
  38.     }   
  39.        
  40.        
  41. }  

 Flow.java

 

Java代码 复制代码 收藏代码
  1. public class Flow   
  2. {   
  3.     private Long id;   
  4.     private String name;   
  5.     private Long classId;   
  6.     private Long sclassId;   
  7.     private Long stepId;   
  8.     private Long actionId;   
  9.     public Long getId()   
  10.     {   
  11.         return id;   
  12.     }   
  13.     public void setId(Long id)   
  14.     {   
  15.         this.id = id;   
  16.     }   
  17.     public String getName()   
  18.     {   
  19.         return name;   
  20.     }   
  21.     public void setName(String name)   
  22.     {   
  23.         this.name = name;   
  24.     }   
  25.     public Long getClassId()   
  26.     {   
  27.         return classId;   
  28.     }   
  29.     public void setClassId(Long classId)   
  30.     {   
  31.         this.classId = classId;   
  32.     }   
  33.     public Long getSclassId()   
  34.     {   
  35.         return sclassId;   
  36.     }   
  37.     public void setSclassId(Long sclassId)   
  38.     {   
  39.         this.sclassId = sclassId;   
  40.     }   
  41.     public Long getStepId()   
  42.     {   
  43.         return stepId;   
  44.     }   
  45.     public void setStepId(Long stepId)   
  46.     {   
  47.         this.stepId = stepId;   
  48.     }   
  49.     public Long getActionId()   
  50.     {   
  51.         return actionId;   
  52.     }   
  53.     public void setActionId(Long actionId)   
  54.     {   
  55.         this.actionId = actionId;   
  56.     }   
  57.        
  58.        
  59.        
  60. }  

posted on 2012-05-14 16:01 透明的鱼 阅读(3941) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: