一、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接口的开发工作量。     三、jaxb代码举例 第一步:需要引入javax.xml.bind.jar 第二步:编写java bean;

  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. publicclass Customer { 
  9.   
  10.     String name; 
  11.     int age; 
  12.     int id; 
  13.   
  14.     public String getName() { 
  15.         return name; 
  16.     } 
  17.   
  18.     @XmlElement 
  19.     publicvoid setName(String name) { 
  20.         this.name = name; 
  21.     } 
  22.   
  23.     publicint getAge() { 
  24.         return age; 
  25.     } 
  26.   
  27.     @XmlElement 
  28.     publicvoid setAge(int age) { 
  29.         this.age = age; 
  30.     } 
  31.   
  32.     publicint getId() { 
  33.         return id; 
  34.     } 
  35.   
  36.     @XmlAttribute 
  37.     publicvoid setId(int id) { 
  38.         this.id = id; 
  39.     } 
  40.   
package com.mkyong.core;
 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Customer {
 
	String name;
	int age;
	int id;
 
	public String getName() {
		return name;
	}
 
	@XmlElement
	public void setName(String name) {
		this.name = name;
	}
 
	public int getAge() {
		return age;
	}
 
	@XmlElement
	public void setAge(int age) {
		this.age = age;
	}
 
	public int getId() {
		return id;
	}
 
	@XmlAttribute
	public void setId(int id) {
		this.id = id;
	}
 
}

第三步:main方法把java bean转化为xml字符串

  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. publicclass JAXBExample { 
  9.     publicstaticvoid 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.     } 
package com.mkyong.core;
 
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class JAXBExample {
	public static void main(String[] args) {
 
	  Customer customer = new Customer();
	  customer.setId(100);
	  customer.setName("mkyong");
	  customer.setAge(29);
 
	  try {
 
		File file = new File("C:\\file.xml");
		JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 
		// output pretty printed
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
		jaxbMarshaller.marshal(customer, file);
		jaxbMarshaller.marshal(customer, System.out);
 
	      } catch (JAXBException e) {
		e.printStackTrace();
	      }
 
	}
}

下面是输出:

  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?> 
  2. <customerid="100"> 
  3.     <age>29</age> 
  4.     <name>mkyong</name> 
  5. </customer> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>29</age>
    <name>mkyong</name>
</customer>

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

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

  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <Useru_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"/> 
<?xml version="1.0" encoding="UTF-8"?>
<User u_id="1" u_name="moto" u_email="aaa@XXX.com" 
	u_mood="今天放假了" u_state="online" u_mobile="12345678901" 
	u_hometown="山西" u_job="IT软件工程师" u_avatar="w34353453543r53" />

第二步:使用trang.jar转化为xsd文件。在命令行执行:

 

  1. java -jar D:\lib\trang.jar user.xml user.xsd 
java -jar D:\lib\trang.jar user.xml user.xsd

下面,是生成的User.java。

  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. publicclass 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid 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.     publicvoid setUState(String value) { 
  297.         this.uState = value; 
  298.     } 
  299.  
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2011.11.13 at 01:26:07 ���� CST 
//


package com.moto.server.bean;

import java.math.BigInteger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType>
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <attribute name="u_avatar" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *       <attribute name="u_email" use="required" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
 *       <attribute name="u_hometown" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *       <attribute name="u_id" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" />
 *       <attribute name="u_job" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *       <attribute name="u_mobile" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" />
 *       <attribute name="u_mood" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *       <attribute name="u_name" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *       <attribute name="u_state" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "User")
public class User {

    @XmlAttribute(name = "u_avatar", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uAvatar;
    @XmlAttribute(name = "u_email", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String uEmail;
    @XmlAttribute(name = "u_hometown", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uHometown;
    @XmlAttribute(name = "u_id", required = true)
    protected BigInteger uId;
    @XmlAttribute(name = "u_job", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uJob;
    @XmlAttribute(name = "u_mobile", required = true)
    protected BigInteger uMobile;
    @XmlAttribute(name = "u_mood", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uMood;
    @XmlAttribute(name = "u_name", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uName;
    @XmlAttribute(name = "u_state", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String uState;

    /**
     * Gets the value of the uAvatar property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUAvatar() {
        return uAvatar;
    }

    /**
     * Sets the value of the uAvatar property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUAvatar(String value) {
        this.uAvatar = value;
    }

    /**
     * Gets the value of the uEmail property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUEmail() {
        return uEmail;
    }

    /**
     * Sets the value of the uEmail property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUEmail(String value) {
        this.uEmail = value;
    }

    /**
     * Gets the value of the uHometown property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUHometown() {
        return uHometown;
    }

    /**
     * Sets the value of the uHometown property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUHometown(String value) {
        this.uHometown = value;
    }

    /**
     * Gets the value of the uId property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getUId() {
        return uId;
    }

    /**
     * Sets the value of the uId property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setUId(BigInteger value) {
        this.uId = value;
    }

    /**
     * Gets the value of the uJob property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUJob() {
        return uJob;
    }

    /**
     * Sets the value of the uJob property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUJob(String value) {
        this.uJob = value;
    }

    /**
     * Gets the value of the uMobile property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getUMobile() {
        return uMobile;
    }

    /**
     * Sets the value of the uMobile property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setUMobile(BigInteger value) {
        this.uMobile = value;
    }

    /**
     * Gets the value of the uMood property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUMood() {
        return uMood;
    }

    /**
     * Sets the value of the uMood property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUMood(String value) {
        this.uMood = value;
    }

    /**
     * Gets the value of the uName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUName() {
        return uName;
    }

    /**
     * Sets the value of the uName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUName(String value) {
        this.uName = value;
    }

    /**
     * Gets the value of the uState property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUState() {
        return uState;
    }

    /**
     * Sets the value of the uState property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUState(String value) {
        this.uState = value;
    }

}
posted on 2013-05-27 14:13  jason_yang  阅读(245)  评论(0编辑  收藏  举报