XML 和 java對象相互轉換

下面使用的是JDK自帶的類,沒有引用任何第三方jar包java

 

Unmarshaller 類使客戶端應用程序可以將 XML 數據轉換爲 Java 內容對象樹。 框架

備註:marshal(序列化、排列、整理) 函數

Marshaller 類使客戶端應用程序可以將 Java 內容樹轉換回 XML 數據。 this

 

Java代碼   收藏代碼
  1. package hb.jaxb;  
  2.   
  3. public class Classroom {  
  4.     private int id;  
  5.     private String name;  
  6.     private int grade;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public int getGrade() {  
  25.         return grade;  
  26.     }  
  27.   
  28.     public void setGrade(int grade) {  
  29.         this.grade = grade;  
  30.     }  
  31.   
  32.     public Classroom(int id, String name, int grade) {  
  33.         super();  
  34.         this.id = id;  
  35.         this.name = name;  
  36.         this.grade = grade;  
  37.     }  
  38.   
  39.     public Classroom() {  
  40.         super();  
  41.     }  
  42.   
  43. }  

 

Java代碼   收藏代碼
  1. package hb.jaxb;  
  2.   
  3. import javax.xml.bind.annotation.XmlRootElement;  
  4.   
  5. @XmlRootElement  
  6. public class Student {  
  7.     private int id;  
  8.     private String name;  
  9.     private int age;  
  10.     private Classroom classroom;  
  11.   
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     public int getAge() {  
  29.         return age;  
  30.     }  
  31.   
  32.     public void setAge(int age) {  
  33.         this.age = age;  
  34.     }  
  35.   
  36.     public Classroom getClassroom() {  
  37.         return classroom;  
  38.     }  
  39.   
  40.     public void setClassroom(Classroom classroom) {  
  41.         this.classroom = classroom;  
  42.     }  
  43.   
  44.     public Student(int id, String name, int age, Classroom classroom) {  
  45.         super();  
  46.         this.id = id;  
  47.         this.name = name;  
  48.         this.age = age;  
  49.         this.classroom = classroom;  
  50.     }  
  51.   
  52.     //無參夠着函數必定須要,不然JXBContext沒法正常解析。  
  53.     public Student() {  
  54.         super();  
  55.     }  
  56. }  

 

注意: spa

一、須要轉換的model對象必定要添加@XmlRootElement註解,其裏面的其餘對象則不須要 .net

二、須要轉換的model對象必定要有不帶參數的構造方法,包括該對象裏面引用的對象ssr

 

Java代碼   收藏代碼
  1. package hb.jaxb;  
  2.   
  3. import java.io.StringReader;  
  4.   
  5. import javax.xml.bind.JAXBContext;  
  6. import javax.xml.bind.JAXBException;  
  7. import javax.xml.bind.Marshaller;  
  8. import javax.xml.bind.Unmarshaller;  
  9. import org.junit.Test;  
  10.   
  11. public class TestJaxb {  
  12.   
  13.     @Test   
  14.     public void beanToXML() {  
  15.         Classroom classroom = new Classroom(1"軟件工程"4);  
  16.         Student student = new Student(101"張三"22, classroom);  
  17.   
  18.         try {  
  19.             JAXBContext context = JAXBContext.newInstance(Student.class);  
  20.             Marshaller marshaller = context.createMarshaller();  
  21.             marshaller.marshal(student, System.out);  
  22.         } catch (JAXBException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.   
  26.     }  
  27.       
  28.     @Test   
  29.     public void XMLStringToBean(){  
  30.         String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>22</age><classroom><grade>4</grade><id>1</id><name>軟件工程</name></classroom><id>101</id><name>張三</name></student>";  
  31.         try {  
  32.             JAXBContext context = JAXBContext.newInstance(Student.class);  
  33.             Unmarshaller unmarshaller = context.createUnmarshaller();  
  34.             Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr));  
  35.             System.out.println(student.getAge());  
  36.             System.out.println(student.getClassroom().getName());  
  37.         } catch (JAXBException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.           
  41.     }  
  42. }  

 

    JAXB(Java Architecture for XML Binding) 是一個業界的標準,是一項能夠根據XML Schema產生Java類的技術。該過程當中,JAXB也提供了將XML實例文檔反向生成Java對象樹的方法,並能將Java對象樹的內容從新寫到XML實例文檔。從另外一方面來說,JAXB提供了快速而簡便的方法將XML模式綁定到Java表示,從而使得Java開發者在Java應用程序中能方便地結合XML數據和處理函數xml

 

    JAXBContext 類提供到 JAXB API 的客戶端入口點。它提供了管理實現 JAXB 綁定框架操做所需的 XML/Java 綁定信息的抽象,這些操做包括:解組、編組和驗證。 對象

相關文章
相關標籤/搜索