javaee5 jaxb 学习

来源:互联网 发布:java 时间加减 编辑:程序博客网 时间:2024/05/17 23:59

下面看看 sun 的 jaxb 2 列子,

可以将 xml schema 的规则 转成 我们的java代码,然后通过 Unmarshaller 来读取 一个xml 实例,这样可以减少我们 解析XML的麻烦。

 

首先创建一个 xml schema   person.xsd

  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2.     <xsd:element name="Person">
  3.         <xsd:complexType>
  4.             <xsd:sequence>
  5.                 <xsd:element name="name" type="xsd:string" />
  6.                 <xsd:element name="sex" type="xsd:string" />
  7.                 <xsd:element name="age" type="xsd:int" />
  8.                 <xsd:element name="tel" type="xsd:string" />
  9.                 <xsd:element name="address" type="xsd:string" />
  10.             </xsd:sequence>
  11.         </xsd:complexType>
  12.     </xsd:element>
  13. </xsd:schema>

 一个简单的Person 定义

 

通过 sun 提供的 xjc 的工具,可以把上面的 schema 转化成 java 代码

jdk 自带了一个,在dos下输入下面命令,, -p  指定生成代码的 包名。

xjc  -p  com.birds.person  person.xsd 

 

主意如果使用 jdk 自带的 xjc 就不需要 ,把 javaee 中jaxb 的包加入到classpath中去,用eclipse 也不需要加入。

jdk 1.6中有 自带的javax.xml.bind 包,

如果使用 javaee 中的 xjc 工具的话 ,要加入下面这些包

jaxb1-impl.jar

jaxb-api.jar

jaxb-impl.jar

jaxb-xjc.jar

 

并且生成对应的代码 需要指定 classpath 使用 com.sun.tools.xjc.XJCTask  来生成。

 

-----

下面是自动生成对应的2个类的代码

  1. package com.birds.person;
  2. import javax.xml.bind.annotation.XmlRegistry;
  3. /**
  4.  * This object contains factory methods for each 
  5.  * Java content interface and Java element interface 
  6.  * generated in the com.birds.person package. 
  7.  * <p>An ObjectFactory allows you to programatically 
  8.  * construct new instances of the Java representation 
  9.  * for XML content. The Java representation of XML 
  10.  * content can consist of schema derived interfaces 
  11.  * and classes representing the binding of schema 
  12.  * type definitions, element declarations and model 
  13.  * groups.  Factory methods for each of these are 
  14.  * provided in this class.
  15.  * 
  16.  */
  17. @XmlRegistry
  18. public class ObjectFactory {
  19.     public ObjectFactory() {
  20.     }
  21.     /**
  22.      * Create an instance of {@link Person }
  23.      * 
  24.      */
  25.     public Person createPerson() {
  26.         return new Person();
  27.     }
  28. }
  1. package com.birds.person;
  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.bind.annotation.XmlElement;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6. import javax.xml.bind.annotation.XmlType;
  7. /**
  8.  * <p>Java class for anonymous complex type.
  9.  * 
  10.  * <p>The following schema fragment specifies the expected content contained within this class.
  11.  * 
  12.  * <pre>
  13.  * <complexType>
  14.  *   <complexContent>
  15.  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
  16.  *       <sequence>
  17.  *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
  18.  *         <element name="sex" type="{http://www.w3.org/2001/XMLSchema}string"/>
  19.  *         <element name="age" type="{http://www.w3.org/2001/XMLSchema}int"/>
  20.  *         <element name="tel" type="{http://www.w3.org/2001/XMLSchema}string"/>
  21.  *         <element name="address" type="{http://www.w3.org/2001/XMLSchema}string"/>
  22.  *       </sequence>
  23.  *     </restriction>
  24.  *   </complexContent>
  25.  * </complexType>
  26.  * </pre>
  27.  * 
  28.  * 
  29.  */
  30. @XmlAccessorType(XmlAccessType.FIELD)
  31. @XmlType(name = "", propOrder = {
  32.     "name",
  33.     "sex",
  34.     "age",
  35.     "tel",
  36.     "address"
  37. })
  38. @XmlRootElement(name = "Person")
  39. public class Person {
  40.     @XmlElement(required = true)
  41.     protected String name;
  42.     @XmlElement(required = true)
  43.     protected String sex;
  44.     protected int age;
  45.     @XmlElement(required = true)
  46.     protected String tel;
  47.     @XmlElement(required = true)
  48.     protected String address;
  49.     /**
  50.      * Gets the value of the name property.
  51.      * 
  52.      * @return
  53.      *     possible object is
  54.      *     {@link String }
  55.      *     
  56.      */
  57.     public String getName() {
  58.         return name;
  59.     }
  60.     /**
  61.      * Sets the value of the name property.
  62.      * 
  63.      * @param value
  64.      *     allowed object is
  65.      *     {@link String }
  66.      *     
  67.      */
  68.     public void setName(String value) {
  69.         this.name = value;
  70.     }
  71.     /**
  72.      * Gets the value of the sex property.
  73.      * 
  74.      * @return
  75.      *     possible object is
  76.      *     {@link String }
  77.      *     
  78.      */
  79.     public String getSex() {
  80.         return sex;
  81.     }
  82.     /**
  83.      * Sets the value of the sex property.
  84.      * 
  85.      * @param value
  86.      *     allowed object is
  87.      *     {@link String }
  88.      *     
  89.      */
  90.     public void setSex(String value) {
  91.         this.sex = value;
  92.     }
  93.     /**
  94.      * Gets the value of the age property.
  95.      * 
  96.      */
  97.     public int getAge() {
  98.         return age;
  99.     }
  100.     /**
  101.      * Sets the value of the age property.
  102.      * 
  103.      */
  104.     public void setAge(int value) {
  105.         this.age = value;
  106.     }
  107.     /**
  108.      * Gets the value of the tel property.
  109.      * 
  110.      * @return
  111.      *     possible object is
  112.      *     {@link String }
  113.      *     
  114.      */
  115.     public String getTel() {
  116.         return tel;
  117.     }
  118.     /**
  119.      * Sets the value of the tel property.
  120.      * 
  121.      * @param value
  122.      *     allowed object is
  123.      *     {@link String }
  124.      *     
  125.      */
  126.     public void setTel(String value) {
  127.         this.tel = value;
  128.     }
  129.     /**
  130.      * Gets the value of the address property.
  131.      * 
  132.      * @return
  133.      *     possible object is
  134.      *     {@link String }
  135.      *     
  136.      */
  137.     public String getAddress() {
  138.         return address;
  139.     }
  140.     /**
  141.      * Sets the value of the address property.
  142.      * 
  143.      * @param value
  144.      *     allowed object is
  145.      *     {@link String }
  146.      *     
  147.      */
  148.     public void setAddress(String value) {
  149.         this.address = value;
  150.     }
  151. }

再写一个 person.xml  数据,目的就是方面的读取xml中的数据

 

  1. <?xml version="1.0"?>
  2. <Person>
  3.    <name>birds</name>
  4.    <sex>man</sex>
  5.    <age>32</age>
  6.    <tel>0949855</tel>
  7.    <address>China,Bejing</address>
  8. </Person>

最后是写个 客户端读取 数据

 

  1.             JAXBContext jc = JAXBContext.newInstance( "com.birds.person" ); // 从生成代码的地方读取,
  2.             Unmarshaller u = jc.createUnmarshaller(); 
  3.             Person po =(Person)u.unmarshal(new FileInputStream( "person.xml" ) );   //加载xml数据文件。
  4.             System.out.println(po.getAddress());
  5.             System.out.println(po.getAge());

 

原创粉丝点击