<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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


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


    網站導航:
     
    主站蜘蛛池模板: 天天影院成人免费观看| 亚洲精品网站在线观看你懂的| 国产精品国产免费无码专区不卡 | 国产成人精品亚洲2020| 亚洲欧洲校园自拍都市| 亚洲激情校园春色| 亚洲大香伊人蕉在人依线| 亚洲人成伊人成综合网久久| 亚洲av无码电影网| 亚洲精品国产综合久久久久紧| 亚洲精品美女久久久久久久| 欧美日韩亚洲精品| 杨幂最新免费特级毛片| 精品一区二区三区免费观看| 中文字幕在线免费播放| 久久免费精彩视频| h片在线免费观看| 在线A级毛片无码免费真人| 免费人成在线观看视频播放| 亚洲综合色在线观看亚洲| 亚洲午夜久久久久久噜噜噜| 久久精品国产精品亚洲色婷婷 | 国产精品免费视频网站| 亚洲国产精品尤物yw在线| 在线亚洲人成电影网站色www| 亚洲春色在线视频| 亚洲丰满熟女一区二区v| 亚洲第一成年网站视频| 成人午夜免费视频| 三年片在线观看免费观看大全动漫| 巨波霸乳在线永久免费视频| 午夜视频在线在免费| 亚洲精品老司机在线观看| 亚洲AV人无码综合在线观看 | 亚洲毛片av日韩av无码| 国产亚洲综合成人91精品| 亚洲成人福利在线观看| 亚洲Av永久无码精品黑人| 嫩草在线视频www免费看| 亚洲视频免费一区| 国产无遮挡吃胸膜奶免费看|