Java對象和XML轉換

Java對象和XML轉換

 

有時候,咱們須要把Java對象轉換成XML文件。這時能夠用JAXB來實現。(JDK1.6及之後的版本無需導入依賴包,由於已經包含在JDK裏了)html

 

假如某個公司有許多部門,每一個部門有許多職員,咱們能夠這樣來設計簡單的bean對象。java

 

[java]  view plain  copy
 
  1. @XmlRootElement(name="department")  
  2. public class Department {  
  3.   
  4.     private String name;    //部門名稱  
  5.     private List<Staff> staffs;           // 其實staff是單復同型,這裏是加's'是爲了區別staff  
  6.       
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     @XmlAttribute  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public List<Staff> getStaffs() {  
  15.         return staffs;  
  16.     }  
  17.     @XmlElement(name="staff")  
  18.     public void setStaffs(List<Staff> staffs) {  
  19.         this.staffs = staffs;  
  20.     }  
  21. }  

 

[java]  view plain  copy
 
  1. @XmlRootElement(name="staff")  
  2. public class Staff {  
  3.   
  4.     private String name;    // 職員名稱  
  5.     private int age;        // 職員年齡  
  6.     private boolean smoker; // 是否爲菸民  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     @XmlElement  
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.     public int getAge() {  
  16.         return age;  
  17.     }  
  18.     @XmlElement  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     public boolean isSmoker() {  
  23.         return smoker;  
  24.     }  
  25.     @XmlAttribute  
  26.     public void setSmoker(boolean smoker) {  
  27.         this.smoker = smoker;  
  28.     }  
  29. }  


下面將生成一個簡單的對象,並轉換成XML字符串。post

 

 

[java]  view plain  copy
 
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) throws Exception {  
  4.           
  5.         JAXBContext context = JAXBContext.newInstance(Department.class,Staff.class);    // 獲取上下文對象  
  6.         Marshaller marshaller = context.createMarshaller(); // 根據上下文獲取marshaller對象  
  7.           
  8.         marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");  // 設置編碼字符集  
  9.         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化XML輸出,有分行和縮進  
  10.           
  11.         marshaller.marshal(getSimpleDepartment(),System.out);   // 打印到控制檯  
  12.           
  13.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  14.         marshaller.marshal(getSimpleDepartment(), baos);  
  15.         String xmlObj = new String(baos.toByteArray());         // 生成XML字符串  
  16.         System.out.println(xmlObj);  
  17.     }  
  18.       
  19.     /** 
  20.      * 生成一個簡單的Department對象 
  21.      * @return 
  22.      */  
  23.     private static Department getSimpleDepartment() {  
  24.         List<Staff> staffs = new ArrayList<Staff>();  
  25.           
  26.         Staff stf = new Staff();  
  27.         stf.setName("周杰倫");  
  28.         stf.setAge(30);  
  29.         stf.setSmoker(false);  
  30.         staffs.add(stf);  
  31.         stf.setName("周筆暢");  
  32.         stf.setAge(28);  
  33.         stf.setSmoker(false);  
  34.         staffs.add(stf);  
  35.         stf.setName("周星馳");  
  36.         stf.setAge(40);  
  37.         stf.setSmoker(true);  
  38.         staffs.add(stf);  
  39.           
  40.         Department dept = new Department();  
  41.         dept.setName("娛樂圈");  
  42.         dept.setStaffs(staffs);  
  43.           
  44.         return dept;  
  45.     }  
  46. }  


控制檯打印信息:this

 

 

[plain]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <department name="娛樂圈">  
  3.     <staff smoker="true">  
  4.         <age>40</age>  
  5.         <name>周星馳</name>  
  6.     </staff>  
  7.     <staff smoker="true">  
  8.         <age>40</age>  
  9.         <name>周星馳</name>  
  10.     </staff>  
  11.     <staff smoker="true">  
  12.         <age>40</age>  
  13.         <name>周星馳</name>  
  14.     </staff>  
  15. </department>  


注意到,咱們能夠用Marshaller.marshal方法將對象轉換成xml文件,也能夠用UnMarshaller.unmarshal將xml轉換成對象。編碼

相關文章
相關標籤/搜索