Xml與Java Object 的轉換[JAXB]

http://a123159521.iteye.com/blog/1987300

博客分類: XMLJAXB
 
Java代碼  收藏代碼
  1. package ycl.learn.xml.jaxb;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9.   
  10. import javax.xml.bind.JAXBContext;  
  11. import javax.xml.bind.JAXBException;  
  12. import javax.xml.bind.Marshaller;  
  13. import javax.xml.bind.Unmarshaller;  
  14.   
  15. public class JAXBUtil<T> {  
  16.   
  17.     @SuppressWarnings("unchecked")  
  18.     public T unmarshal(Class<T> clazz, InputStream is) throws JAXBException {  
  19.         JAXBContext context = JAXBContext.newInstance(clazz);  
  20.         Unmarshaller un = context.createUnmarshaller();  
  21.         return (T) un.unmarshal(is);  
  22.     }  
  23.       
  24.     public T unmarshal(Class<T> clazz, File file) throws JAXBException, FileNotFoundException {  
  25.         return unmarshal(clazz,new FileInputStream(file));  
  26.     }  
  27.       
  28.     public void marshal(T element,OutputStream os) throws JAXBException{  
  29.         JAXBContext jc = JAXBContext.newInstance(element.getClass());  
  30.         Marshaller m = jc.createMarshaller();  
  31.         m.marshal(element, os);  
  32.     }  
  33.       
  34.     public void marshal(T element, File output) throws FileNotFoundException, JAXBException{  
  35.         marshal(element,new FileOutputStream(output));  
  36.     }  
  37.       
  38.    
  39. }  


this is the simple util that packaging the JAXB operator.
then we can use it simplely.

1. we just export simple JODO to xml
Java代碼  收藏代碼
  1. package ycl.learn.xml.jaxb;  
  2.   
  3. import javax.xml.bind.annotation.XmlRootElement;  
  4.    
  5. @XmlRootElement   
  6. public class EmployeeDO {  
  7.     private int id;  
  8.   
  9.     private String gender;  
  10.   
  11.     private int age;  
  12.   
  13.     private String name;  
  14.   
  15.     private String role;  
  16.   
  17.     private String password;  
  18.    
  19.     public String getPassword() {  
  20.         return password;  
  21.     }  
  22.   
  23.     public void setPassword(String password) {  
  24.         this.password = password;  
  25.     }  
  26.    
  27.     public int getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(int id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public int getAge() {  
  36.         return age;  
  37.     }  
  38.   
  39.     public void setAge(int age) {  
  40.         this.age = age;  
  41.     }  
  42.   
  43.     public String getName() {  
  44.         return name;  
  45.     }  
  46.   
  47.     public void setName(String name) {  
  48.         this.name = name;  
  49.     }  
  50.    
  51.     public String getGender() {  
  52.         return gender;  
  53.     }  
  54.   
  55.     public void setGender(String gender) {  
  56.         this.gender = gender;  
  57.     }  
  58.   
  59.     public String getRole() {  
  60.         return role;  
  61.     }  
  62.   
  63.     public void setRole(String role) {  
  64.         this.role = role;  
  65.     }  
  66.   
  67.     @Override  
  68.     public String toString() {  
  69.         return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER="  
  70.                 + gender + " ROLE=" + role + " PASSWORD=" + password;  
  71.     }  
  72. }  


we must add @XmlRootElement to point this object is the root object for export.
Java代碼  收藏代碼
  1. package ycl.learn.xml.jaxb;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5.   
  6. import javax.xml.bind.JAXBException;  
  7.   
  8. public class JAXBUtilTest {  
  9.     private static final String FILE_NAME = "jaxb-emp-jaxbutil.xml";  
  10.     /** 
  11.      * @param args 
  12.      * @throws JAXBException  
  13.      * @throws FileNotFoundException  
  14.      */  
  15.     public static void main(String[] args) throws FileNotFoundException, JAXBException {  
  16.         // TODO Auto-generated method stub  
  17.         EmployeeDO emp = new EmployeeDO();  
  18.         emp.setId(1);  
  19.         emp.setAge(25);  
  20.         emp.setName("Pankaj");  
  21.         emp.setGender("Male");  
  22.         emp.setRole("Developer");  
  23.         emp.setPassword("sensitive");  
  24.         JAXBUtil<EmployeeDO> ju = new JAXBUtil<EmployeeDO>();  
  25.         ju.marshal(emp, new File(FILE_NAME));  
  26.         System.out.println("generator success");  
  27.         EmployeeDO empant = ju.unmarshal(EmployeeDO.classnew File(FILE_NAME));  
  28.         System.out.println("reader success");  
  29.         System.out.println(empant);  
  30.     }  
  31.   
  32. }  


we can easily use JAXBUtil to marshal Object to file.
we can also easily use JAXBUtil to unmarchal File to Object.

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <employeeDO>  
  3.     <age>25</age>  
  4.     <gender>Male</gender>  
  5.     <id>1</id>  
  6.     <name>Pankaj</name>  
  7.     <password>sensitive</password>  
  8.     <role>Developer</role>  
  9. </employeeDO>  


If you review this code carefully, you will find the default root element name is the Object's name with first letter is lowercase.
the element under the root element is the attribute of the object.
Q1: can i change the Element Name, to generator Xml
Java代碼  收藏代碼
  1. package ycl.learn.xml.jaxb;  
  2.   
  3. import javax.xml.bind.annotation.XmlElement;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5.    
  6. @XmlRootElement(name="content")   
  7. public class EmployeeDO {  
  8.     private int id;  
  9.   
  10.     private String gender;  
  11.   
  12.     private int age;  
  13.   
  14.     private String name;  
  15.   
  16.     private String role;  
  17.   
  18.     private String password;  
  19.    
  20.     @XmlElement(name="pwd")  
  21.     public String getPassword() {  
  22.         return password;  
  23.     }  
  24.   
  25.     public void setPassword(String password) {  
  26.         this.password = password;  
  27.     }  
  28.    
  29.     public int getId() {  
  30.         return id;  
  31.     }  
  32.   
  33.     public void setId(int id) {  
  34.         this.id = id;  
  35.     }  
  36.   
  37.     public int getAge() {  
  38.         return age;  
  39.     }  
  40.   
  41.     public void setAge(int age) {  
  42.         this.age = age;  
  43.     }  
  44.   
  45.     public String getName() {  
  46.         return name;  
  47.     }  
  48.   
  49.     public void setName(String name) {  
  50.         this.name = name;  
  51.     }  
  52.    
  53.     public String getGender() {  
  54.         return gender;  
  55.     }  
  56.   
  57.     public void setGender(String gender) {  
  58.         this.gender = gender;  
  59.     }  
  60.   
  61.     public String getRole() {  
  62.         return role;  
  63.     }  
  64.   
  65.     public void setRole(String role) {  
  66.         this.role = role;  
  67.     }  
  68.   
  69.     @Override  
  70.     public String toString() {  
  71.         return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER="  
  72.                 + gender + " ROLE=" + role + " PASSWORD=" + password;  
  73.     }  
  74. }  

we just need to set the @XmlRootElement or @XmlElement's name is ok.then the xml content is
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <content>  
  3.     <age>25</age>  
  4.     <gender>Male</gender>  
  5.     <id>1</id>  
  6.     <name>Pankaj</name>  
  7.     <pwd>sensitive</pwd>  
  8.     <role>Developer</role>  
  9. </content>  


Q2: can i point that some attribute of Object i don't want to export

Java代碼  收藏代碼
  1. package ycl.learn.xml.jaxb;  
  2.   
  3. import javax.xml.bind.annotation.XmlElement;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5. import javax.xml.bind.annotation.XmlTransient;  
  6.    
  7. @XmlRootElement(name="content")   
  8. public class EmployeeDO {  
  9.     private int id;  
  10.   
  11.     private String gender;  
  12.   
  13.     private int age;  
  14.   
  15.     private String name;  
  16.   
  17.     private String role;  
  18.   
  19.     private String password;  
  20.    
  21.     @XmlElement(name="pwd")  
  22.     public String getPassword() {  
  23.         return password;  
  24.     }  
  25.   
  26.     public void setPassword(String password) {  
  27.         this.password = password;  
  28.     }  
  29.    
  30.     @XmlTransient  
  31.     public int getId() {  
  32.         return id;  
  33.     }  
  34.   
  35.     public void setId(int id) {  
  36.         this.id = id;  
  37.     }  
  38.   
  39.     public int getAge() {  
  40.         return age;  
  41.     }  
  42.   
  43.     public void setAge(int age) {  
  44.         this.age = age;  
  45.     }  
  46.   
  47.     public String getName() {  
  48.         return name;  
  49.     }  
  50.   
  51.     public void setName(String name) {  
  52.         this.name = name;  
  53.     }  
  54.    
  55.     public String getGender() {  
  56.         return gender;  
  57.     }  
  58.   
  59.     public void setGender(String gender) {  
  60.         this.gender = gender;  
  61.     }  
  62.   
  63.     public String getRole() {  
  64.         return role;  
  65.     }  
  66.   
  67.     public void setRole(String role) {  
  68.         this.role = role;  
  69.     }  
  70.   
  71.     @Override  
  72.     public String toString() {  
  73.         return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER="  
  74.                 + gender + " ROLE=" + role + " PASSWORD=" + password;  
  75.     }  
  76. }  


you can add @XmlTransient before the getMethod, then this attribute won't be export.
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <content>  
  3.     <age>25</age>  
  4.     <gender>Male</gender>  
  5.     <name>Pankaj</name>  
  6.     <pwd>sensitive</pwd>  
  7.     <role>Developer</role>  
  8. </content>  


Q3: can i export the attribute of Object as attribute, not Element of xml, and change the attribute name

Java代碼  收藏代碼
  1. @XmlAttribute(name="agefather")  
  2.     public int getAge() {  
  3.         return age;  
  4.     }  

very easily to change sub element to attribute.

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <content agefather="25">  
  3.     <gender>Male</gender>  
  4.     <name>Pankaj</name>  
  5.     <pwd>sensitive</pwd>  
  6.     <role>Developer</role>  
  7. </content>  


Q4: i want to export the xml element as my order

Java代碼  收藏代碼
  1. @XmlRootElement(name="content")   
  2. @XmlType(propOrder={"gender","age","name","role","password"})  
  3. public class EmployeeDO   


Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <content agefather="25">  
  3.     <gender>Male</gender>  
  4.     <name>Pankaj</name>  
  5.     <role>Developer</role>  
  6.     <pwd>sensitive</pwd>  
  7. </content>  


you can see this element is ordered by our defined.

Q5: i want to export list of Objects

Java代碼  收藏代碼
  1.         @XmlElementWrapper(name="employees")  
  2. @XmlElement(name="employee")  
  3. public List<Employee> getEmployees() {  
  4.     return employees;  
  5. }  


Java代碼  收藏代碼
  1. <employees>  
  2.         <employee>  
  3.             <age>25</age>  
  4.             <gender>GGGGGG</gender>  
  5.             <id>1</id>  
  6.             <name>Pankaj</name>  
  7.             <password>sensitive</password>  
  8.             <role>Developer</role>   
  9.             <family></family>  
  10.         </employee>  
  11.         <employee>  
  12.             <age>25</age>  
  13.             <gender>GGGGGGG</gender>  
  14.             <id>1</id>  
  15.             <name>Pankaj</name>  
  16.             <password>sensitive</password>  
  17.             <role>Developer</role>   
  18.             <family></family>  
  19.         </employee>  
  20.     </employees>  


Q5: i want to export list of Objects in other Object

Java代碼  收藏代碼
  1. [ClassA]  
  2. public Family getFamily() {  
  3.         return family;  
  4.     }  


Java代碼  收藏代碼
  1. public class Family {  
  2.   
  3.     private List<Employee> employees;  
  4.       
  5.     @XmlElement(name="employee")  
  6.     public List<Employee> getEmployees() {  
  7.         return employees;  
  8.     }  
  9.   
  10.     public void setEmployees(List<Employee> employees) {  
  11.         this.employees = employees;  
  12.     }   
  13. }  

Java代碼  收藏代碼
  1. <family>  
  2.         <employee>  
  3.             <age>25</age>  
  4.             <gender>LLLLLLL</gender>  
  5.             <id>1</id>  
  6.             <name>Pankaj</name>  
  7.             <password>sensitive</password>  
  8.             <role>Developer</role>   
  9.             <family></family>  
  10.         </employee>  
  11.         <employee>  
  12.             <age>25</age>  
  13.             <gender>Male</gender>  
  14.             <id>1</id>  
  15.             <name>Pankaj</name>  
  16.             <password>sensitive</password>  
  17.             <role>Developer</role>   
  18.             <family></family>  
  19.         </employee>  
  20.     </family>  


oh, you just don't to defined family, because it is the element of the object, default define is ok.
the employee is multiple in family, you just define the @XmlElement empoyee is ok,and this will be auto add to list

so this is you can think of the four questions, and i can't think other situations, and i think this four points can resolve all problems that we encountered, we can easily change xml Element name, Element attribute, Element order, that's cool.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章