JAXB: XML 与 Java之间的映射 (OXM)

来源:互联网 发布:中兴网络培训 编辑:程序博客网 时间:2024/04/30 01:32
一、jaxb是什么 
    JAXB是Java Architecture for XML Binding的缩写。可以将一个Java对象转变成为XML格式,反之亦然。 
    我们把对象与关系数据库之间的映射称为ORM,其实也可以把对象与XML之间的映射称为OXM(Object XML Mapping)。原来JAXB是Java EE的一部分,在JDK1.6中,SUN将其放到了Java SE中,这也是SUN的一贯做法。JDK1.6中自带的这个JAXB版本是2.0,比起1.0(JSR 31)来,JAXB2(JSR 222)用JDK5的新特性Annotation来标识要作绑定的类和属性等,这就极大简化了开发的工作量。 
    二、jaxb应用模式 

    在JAVA EE 5\6中,jaxb可以很方便的与jax-rs、jax-ws集成,极大的简化了web service接口的开发工作量。 

三、需要的JAR包: eclipselink

  你需要在eclipse中你的项目中引入支持jaxb的jar包,目前的eclipselink2.5.2 or 2.6.x都支持它,下载page如下 

https://www.eclipse.org/eclipselink/downloads/previous_releases.php

或者利用"eclipse marketplace" or "install new software" 去自动安装即可。

而且这个jar可以支持JPA等,不错的工具。


jaxb代码举例 
第一步:需要引入eclipselink等支持JAXB的包
第二步:编写java bean; 

[java] view plain copy
  1. package com.mkyong.core;  
  2.    
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.    
  7. @XmlRootElement  
  8. public class Customer {  
  9.    
  10.     String name;  
  11.     int age;  
  12.     int id;  
  13.    
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.    
  18.     @XmlElement  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.    
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.    
  27.     @XmlElement  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.    
  32.     public int getId() {  
  33.         return id;  
  34.     }  
  35.    
  36.     @XmlAttribute  
  37.     public void setId(int id) {  
  38.         this.id = id;  
  39.     }  
  40.    
  41. }  

第三步:main方法把java bean转化为xml字符串 
[java] view plain copy
  1. package com.mkyong.core;  
  2.    
  3. import java.io.File;  
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Marshaller;  
  7.    
  8. public class JAXBExample {  
  9.     public static void main(String[] args) {  
  10.    
  11.       Customer customer = new Customer();  
  12.       customer.setId(100);  
  13.       customer.setName("mkyong");  
  14.       customer.setAge(29);  
  15.    
  16.       try {  
  17.    
  18.         File file = new File("C:\\file.xml");  
  19.         JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
  20.         Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  21.    
  22.         // output pretty printed  
  23.         jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  24.    
  25.         jaxbMarshaller.marshal(customer, file);  
  26.         jaxbMarshaller.marshal(customer, System.out);  
  27.    
  28.           } catch (JAXBException e) {  
  29.         e.printStackTrace();  
  30.           }  
  31.    
  32.     }  
  33. }  

下面是输出: 
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer id="100">  
  3.     <age>29</age>  
  4.     <name>mkyong</name>  
  5. </customer>  


jdk提供了xjc工具可以使xsd自动生成相应的java bean,这大大提高了开发的效率。同时,我们也可以使用trang.jar把xml轻松转化为xsd。下面是使用的举例。 

    第一步:把数据库表映射为xml 

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <User u_id="1" u_name="moto" u_email="aaa@XXX.com"   
  3.     u_mood="今天放假了" u_state="online" u_mobile="12345678901"   
  4.     u_hometown="山西" u_job="IT软件工程师" u_avatar="w34353453543r53" />  
第二步:使用trang.jar转化为xsd文件。在命令行执行: 

[html] view plain copy
  1. java -jar D:\lib\trang.jar user.xml user.xsd  
下面,是生成的User.java。 
[java] view plain copy
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2011.11.13 at 01:26:07 ���� CST   
  6. //  
  7.   
  8.   
  9. package com.moto.server.bean;  
  10.   
  11. import java.math.BigInteger;  
  12. import javax.xml.bind.annotation.XmlAccessType;  
  13. import javax.xml.bind.annotation.XmlAccessorType;  
  14. import javax.xml.bind.annotation.XmlAttribute;  
  15. import javax.xml.bind.annotation.XmlRootElement;  
  16. import javax.xml.bind.annotation.XmlSchemaType;  
  17. import javax.xml.bind.annotation.XmlType;  
  18. import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;  
  19. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;  
  20.   
  21.   
  22. /** 
  23.  * <p>Java class for anonymous complex type. 
  24.  *  
  25.  * <p>The following schema fragment specifies the expected content contained within this class. 
  26.  *  
  27.  * <pre> 
  28.  * <complexType> 
  29.  *   <complexContent> 
  30.  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  31.  *       <attribute name="u_avatar" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  32.  *       <attribute name="u_email" use="required" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" /> 
  33.  *       <attribute name="u_hometown" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  34.  *       <attribute name="u_id" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" /> 
  35.  *       <attribute name="u_job" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  36.  *       <attribute name="u_mobile" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" /> 
  37.  *       <attribute name="u_mood" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  38.  *       <attribute name="u_name" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  39.  *       <attribute name="u_state" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" /> 
  40.  *     </restriction> 
  41.  *   </complexContent> 
  42.  * </complexType> 
  43.  * </pre> 
  44.  *  
  45.  *  
  46.  */  
  47. @XmlAccessorType(XmlAccessType.FIELD)  
  48. @XmlType(name = "")  
  49. @XmlRootElement(name = "User")  
  50. public class User {  
  51.   
  52.     @XmlAttribute(name = "u_avatar", required = true)  
  53.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  54.     @XmlSchemaType(name = "NCName")  
  55.     protected String uAvatar;  
  56.     @XmlAttribute(name = "u_email", required = true)  
  57.     @XmlSchemaType(name = "anySimpleType")  
  58.     protected String uEmail;  
  59.     @XmlAttribute(name = "u_hometown", required = true)  
  60.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  61.     @XmlSchemaType(name = "NCName")  
  62.     protected String uHometown;  
  63.     @XmlAttribute(name = "u_id", required = true)  
  64.     protected BigInteger uId;  
  65.     @XmlAttribute(name = "u_job", required = true)  
  66.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  67.     @XmlSchemaType(name = "NCName")  
  68.     protected String uJob;  
  69.     @XmlAttribute(name = "u_mobile", required = true)  
  70.     protected BigInteger uMobile;  
  71.     @XmlAttribute(name = "u_mood", required = true)  
  72.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  73.     @XmlSchemaType(name = "NCName")  
  74.     protected String uMood;  
  75.     @XmlAttribute(name = "u_name", required = true)  
  76.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  77.     @XmlSchemaType(name = "NCName")  
  78.     protected String uName;  
  79.     @XmlAttribute(name = "u_state", required = true)  
  80.     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)  
  81.     @XmlSchemaType(name = "NCName")  
  82.     protected String uState;  
  83.   
  84.     /** 
  85.      * Gets the value of the uAvatar property. 
  86.      *  
  87.      * @return 
  88.      *     possible object is 
  89.      *     {@link String } 
  90.      *      
  91.      */  
  92.     public String getUAvatar() {  
  93.         return uAvatar;  
  94.     }  
  95.   
  96.     /** 
  97.      * Sets the value of the uAvatar property. 
  98.      *  
  99.      * @param value 
  100.      *     allowed object is 
  101.      *     {@link String } 
  102.      *      
  103.      */  
  104.     public void setUAvatar(String value) {  
  105.         this.uAvatar = value;  
  106.     }  
  107.   
  108.     /** 
  109.      * Gets the value of the uEmail property. 
  110.      *  
  111.      * @return 
  112.      *     possible object is 
  113.      *     {@link String } 
  114.      *      
  115.      */  
  116.     public String getUEmail() {  
  117.         return uEmail;  
  118.     }  
  119.   
  120.     /** 
  121.      * Sets the value of the uEmail property. 
  122.      *  
  123.      * @param value 
  124.      *     allowed object is 
  125.      *     {@link String } 
  126.      *      
  127.      */  
  128.     public void setUEmail(String value) {  
  129.         this.uEmail = value;  
  130.     }  
  131.   
  132.     /** 
  133.      * Gets the value of the uHometown property. 
  134.      *  
  135.      * @return 
  136.      *     possible object is 
  137.      *     {@link String } 
  138.      *      
  139.      */  
  140.     public String getUHometown() {  
  141.         return uHometown;  
  142.     }  
  143.   
  144.     /** 
  145.      * Sets the value of the uHometown property. 
  146.      *  
  147.      * @param value 
  148.      *     allowed object is 
  149.      *     {@link String } 
  150.      *      
  151.      */  
  152.     public void setUHometown(String value) {  
  153.         this.uHometown = value;  
  154.     }  
  155.   
  156.     /** 
  157.      * Gets the value of the uId property. 
  158.      *  
  159.      * @return 
  160.      *     possible object is 
  161.      *     {@link BigInteger } 
  162.      *      
  163.      */  
  164.     public BigInteger getUId() {  
  165.         return uId;  
  166.     }  
  167.   
  168.     /** 
  169.      * Sets the value of the uId property. 
  170.      *  
  171.      * @param value 
  172.      *     allowed object is 
  173.      *     {@link BigInteger } 
  174.      *      
  175.      */  
  176.     public void setUId(BigInteger value) {  
  177.         this.uId = value;  
  178.     }  
  179.   
  180.     /** 
  181.      * Gets the value of the uJob property. 
  182.      *  
  183.      * @return 
  184.      *     possible object is 
  185.      *     {@link String } 
  186.      *      
  187.      */  
  188.     public String getUJob() {  
  189.         return uJob;  
  190.     }  
  191.   
  192.     /** 
  193.      * Sets the value of the uJob property. 
  194.      *  
  195.      * @param value 
  196.      *     allowed object is 
  197.      *     {@link String } 
  198.      *      
  199.      */  
  200.     public void setUJob(String value) {  
  201.         this.uJob = value;  
  202.     }  
  203.   
  204.     /** 
  205.      * Gets the value of the uMobile property. 
  206.      *  
  207.      * @return 
  208.      *     possible object is 
  209.      *     {@link BigInteger } 
  210.      *      
  211.      */  
  212.     public BigInteger getUMobile() {  
  213.         return uMobile;  
  214.     }  
  215.   
  216.     /** 
  217.      * Sets the value of the uMobile property. 
  218.      *  
  219.      * @param value 
  220.      *     allowed object is 
  221.      *     {@link BigInteger } 
  222.      *      
  223.      */  
  224.     public void setUMobile(BigInteger value) {  
  225.         this.uMobile = value;  
  226.     }  
  227.   
  228.     /** 
  229.      * Gets the value of the uMood property. 
  230.      *  
  231.      * @return 
  232.      *     possible object is 
  233.      *     {@link String } 
  234.      *      
  235.      */  
  236.     public String getUMood() {  
  237.         return uMood;  
  238.     }  
  239.   
  240.     /** 
  241.      * Sets the value of the uMood property. 
  242.      *  
  243.      * @param value 
  244.      *     allowed object is 
  245.      *     {@link String } 
  246.      *      
  247.      */  
  248.     public void setUMood(String value) {  
  249.         this.uMood = value;  
  250.     }  
  251.   
  252.     /** 
  253.      * Gets the value of the uName property. 
  254.      *  
  255.      * @return 
  256.      *     possible object is 
  257.      *     {@link String } 
  258.      *      
  259.      */  
  260.     public String getUName() {  
  261.         return uName;  
  262.     }  
  263.   
  264.     /** 
  265.      * Sets the value of the uName property. 
  266.      *  
  267.      * @param value 
  268.      *     allowed object is 
  269.      *     {@link String } 
  270.      *      
  271.      */  
  272.     public void setUName(String value) {  
  273.         this.uName = value;  
  274.     }  
  275.   
  276.     /** 
  277.      * Gets the value of the uState property. 
  278.      *  
  279.      * @return 
  280.      *     possible object is 
  281.      *     {@link String } 
  282.      *      
  283.      */  
  284.     public String getUState() {  
  285.         return uState;  
  286.     }  
  287.   
  288.     /** 
  289.      * Sets the value of the uState property. 
  290.      *  
  291.      * @param value 
  292.      *     allowed object is 
  293.      *     {@link String } 
  294.      *      
  295.      */  
  296.     public void setUState(String value) {  
  297.         this.uState = value;  
  298.     }  
  299.   
  300. }  

另外也可以利用eclipse的jaxb项目自己的菜单功能也可以根据 java class自动生成xsd文件,如下图



0 0
原创粉丝点击