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 透明的魚 閱讀(3950) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 上犹县| 衡东县| 邮箱| 新疆| 平南县| 文昌市| 新郑市| 灌云县| 达拉特旗| 偏关县| 台南县| 永福县| 揭西县| 万安县| 东乡| 海阳市| 伽师县| 衡南县| 洛扎县| 松阳县| 当雄县| 宁夏| 淮北市| 高尔夫| 长海县| 郴州市| 宜良县| 房产| 哈尔滨市| 莱州市| 武夷山市| 泸溪县| 邻水| 钟祥市| 荥阳市| 东乌珠穆沁旗| 揭西县| 明光市| 湾仔区| 蒙山县| 阳谷县|