JAXB註解使用

一.Jaxb處理java對象和xml之間轉換經常使用的annotation有:

  1. @XmlType
  2. @XmlElement
  3. @XmlRootElement
  4. @XmlAttribute
  5. @XmlAccessorType
  6. @XmlAccessorOrder
  7. @XmlTransient
  8. @XmlJavaTypeAdapter

 二.經常使用annotation使用說明

 

  1. @XmlType

  @XmlType用在class類的註解,常與@XmlRootElement,@XmlAccessorType一塊兒使用。它有三個屬性:name、propOrder、namespace,常用的只有前兩個屬性。如:html

@XmlType(name = "basicStruct", propOrder = {
    "intValue",
    "stringArray",
    "stringValue"
)
在使用@XmlType的propOrder 屬性時,必須列出JavaBean對象中的全部屬性,不然會報錯。

  2.@XmlElement

  @XmlElement將Java對象的屬性映射爲xml的節點,在使用@XmlElement時,可經過name屬性改變java對象屬性在xml中顯示的名稱。如:java

  @XmlElement(name="Address")  app

  private String yourAddress;ide

  3.@XmlRootElement

  @XmlRootElement用於類級別的註解,對應xml的跟元素,常與 @XmlType 和 @XmlAccessorType一塊兒使用。如:this

  @XmlType
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlRootElement
  public class Address {}spa

  4.@XmlAttribute

  @XmlAttribute用於把java對象的屬性映射爲xml的屬性,並可經過name屬性爲生成的xml屬性指定別名。如:.net

  @XmlAttribute(name="Country")
  private String state;code

  5.@XmlAccessorType

  @XmlAccessorType用於指定由java對象生成xml文件時對java對象屬性的訪問方式。常與@XmlRootElement、@XmlType一塊兒使用。它的屬性值是XmlAccessType的4個枚舉值,分   別爲:orm

  XmlAccessType.FIELD:java對象中的全部成員變量xml

  XmlAccessType.PROPERTY:java對象中全部經過getter/setter方式訪問的成員變量

  XmlAccessType.PUBLIC_MEMBER:java對象中全部的public訪問權限的成員變量和經過getter/setter方式訪問的成員變量

  XmlAccessType.NONE:java對象的全部屬性都不映射爲xml的元素

  注意:@XmlAccessorType的默認訪問級別是XmlAccessType.PUBLIC_MEMBER,所以,若是java對象中的private成員變量設置了public權限的getter/setter方法,就不要在   private變量上使用@XmlElement和@XmlAttribute註解,不然在由java對象生成xml時會報同一個屬性在java類裏存在兩次的錯誤。同理,若是@XmlAccessorType的訪問權限   爲XmlAccessType.NONE,若是在java的成員變量上使用了@XmlElement或@XmlAttribute註解,這些成員變量依然能夠映射到xml文件。

  6.@XmlAccessorOrder

  @XmlAccessorOrder用於對java對象生成的xml元素進行排序。它有兩個屬性值:

  AccessorOrder.ALPHABETICAL:對生成的xml元素按字母書序排序

  XmlAccessOrder.UNDEFINED:不排序

  7.@XmlTransient

  @XmlTransient用於標示在由java對象映射xml時,忽略此屬性。即,在生成的xml文件中不出現此元素。

  8.@XmlJavaTypeAdapter

  @XmlJavaTypeAdapter經常使用在轉換比較複雜的對象時,如map類型或者格式化日期等。使用此註解時,須要本身寫一個adapter類繼承XmlAdapter抽象類,並實現裏面的方法。

  @XmlJavaTypeAdapter(value=xxx.class),value爲本身定義的adapter類

  XmlAdapter以下:

public abstract class XmlAdapter<ValueType,BoundType> {
    // Do-nothing constructor for the derived classes.
    protected XmlAdapter() {}
    // Convert a value type to a bound type.
    public abstract BoundType unmarshal(ValueType v);
    // Convert a bound type to a value type.
    public abstract ValueType marshal(BoundType v);
 }

 三.示例

  1.Shop.java

?

package jaxb.shop;

 

import java.util.Set;

 

import javax.xml.bind.annotation.XmlAccessOrder;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

import javax.xml.bind.annotation.XmlElementWrapper;

import javax.xml.bind.annotation.XmlAccessorOrder;

 

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name ="shop", propOrder = {"name""number","describer""address","orders"})

?

@XmlRootElement(name ="CHMart")

public class Shop {

 

    @XmlAttribute

    privateString name;

 

    // @XmlElement

    privateString number;

 

    @XmlElement

    privateString describer;

 

    @XmlElementWrapper(name ="orders")

    @XmlElement(name ="order")

    privateSet<Order> orders;

 

    @XmlElement

    privateAddress address;

 

    publicShop() {

    }

 

    publicShop(String name, String number, String describer, Address address) {

        this.name = name;

        this.number = number;

        this.describer = describer;

        this.address = address;

    }

 

    getter/setter略

?

//同時使用了@XmlType(propOrder={})和@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL),可是生成的xml只按照propOrder定義的順序生成元素

  2.Order.java

?

package jaxb.shop;

 

import java.math.BigDecimal;

import java.util.Date;

 

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.XmlType;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

 

@XmlType(name="order",propOrder={"shopName","orderNumber","price","amount","purDate","customer"})

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement

public class Order {

 

//  @XmlElement  

    privateString shopName;

 

    @XmlAttribute

    privateString orderNumber;

 

//  @XmlElement

    @XmlJavaTypeAdapter(value=DateAdapter.class)

    privateDate purDate;

 

//  @XmlElement

    privateBigDecimal price;

 

//  @XmlElement

    privateint amount;

 

//  @XmlElement

    privateCustomer customer;

 

    publicOrder() {

    }

 

    publicOrder(String shopName, String orderNumber, Date purDate,

            BigDecimal price,int amount) {

        this.shopName = shopName;

        this.orderNumber = orderNumber;

        this.purDate = purDate;

        this.price = price;

        this.amount = amount;

    }

?

getter/setter略

?

//@XmlAccessorType(XmlAccessType.FIELD),因此此處註釋掉了@XmlElement,xml中依然會生成這些元素

  3.Customer.java

?

package jaxb.shop;

 

import java.util.Set;

import javax.xml.bind.annotation.XmlType;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

 

@XmlType

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement

public class Customer {

 

    @XmlAttribute

    privateString name;

 

    privateString gender;

 

    privateString phoneNo;

 

    privateAddress address;

 

    privateSet<Order> orders;

 

    publicCustomer() {

    }

 

    publicCustomer(String name, String gender, String phoneNo, Address address) {

        this.name = name;

        this.gender = gender;

        this.phoneNo = phoneNo;

        this.address = address;

    }

?

getter/setter略

  4.Address.java

?

package jaxb.shop;

 

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlAccessOrder;

import javax.xml.bind.annotation.XmlAccessorOrder;

 

@XmlType(propOrder={"state","province","city","street","zip"})

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)

@XmlAccessorType(XmlAccessType.NONE)

@XmlRootElement

public class Address {

 

    @XmlAttribute 

    privateString state;

     

    @XmlElement

    privateString province;

     

    @XmlElement

    privateString city;

 

    @XmlElement

    privateString street;

     

    @XmlElement

    privateString zip;

 

    publicAddress() {

        super();

    }

 

    publicAddress(String state, String province, String city, String street,

            String zip) {

        super();

        this.state = state;

        this.province = province;

        this.city = city;

        this.street = street;

        this.zip = zip;

    }

?

getter/setter略

?

//注意:雖然@XmlAccessorType爲XmlAccessType.NONE,可是在java類的私有屬性上加了@XmlAttribute和@XmlElement註解後,這些私有成員會映射生成xml的元素

  5.DateAdapter.java

?

package jaxb.shop;

 

import java.util.Date;

import java.text.SimpleDateFormat;

 

import javax.xml.bind.annotation.adapters.XmlAdapter;

 

public class DateAdapter extendsXmlAdapter<String, Date> {

 

    privateString pattern = "yyyy-MM-dd HH:mm:ss";

    SimpleDateFormat fmt =new SimpleDateFormat(pattern);

     

    @Override

    publicDate unmarshal(String dateStr) throwsException {

         

        returnfmt.parse(dateStr);

    }

 

    @Override

    publicString marshal(Date date) throwsException {

         

        returnfmt.format(date);

    }

 

}

?

//用於格式化日期在xml中的顯示格式,而且由xml unmarshal爲java對象時,將字符串解析爲Date對象

  6.ShopTest.java

package jaxb.shop;



import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.math.BigDecimal;

import java.util.Date;

import java.util.HashSet;

import java.util.Set;



import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;



public class ShopTest {



    publicstatic void main(String[] args) throws JAXBException, IOException{

        Set<Order> orders =new HashSet<Order>();

         

        Address address1 =new Address("China","ShangHai", "ShangHai", "Huang","200000");

        Customer customer1 =new Customer("Jim","male", "13699990000", address1);

        Order order1 =new Order("Mart","LH59900", new Date(), newBigDecimal(60),1);

        order1.setCustomer(customer1);

         

        Address address2 =new Address("China","JiangSu", "NanJing", "ZhongYangLu","210000");

        Customer customer2 =new Customer("David","male", "13699991000", address2);

        Order order2 =new Order("Mart","LH59800", new Date(), newBigDecimal(80),1);

        order2.setCustomer(customer2);

         

        orders.add(order1);

        orders.add(order2);

         

        Address address3 =new Address("China","ZheJiang", "HangZhou", "XiHuRoad","310000");

        Shop shop =new Shop("CHMart","100000", "EveryThing",address3);

        shop.setOrder(orders);

         

         

        FileWriter writer =null;

        JAXBContext context = JAXBContext.newInstance(Shop.class);

        try{

            Marshaller marshal = context.createMarshaller();

            marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

            marshal.marshal(shop, System.out);

             

            writer =new FileWriter("shop.xml");

            marshal.marshal(shop, writer);

        }catch (Exception e) {

            e.printStackTrace();

        }

         

        Unmarshaller unmarshal = context.createUnmarshaller();

        FileReader reader =new FileReader("shop.xml") ;

        Shop shop1 = (Shop)unmarshal.unmarshal(reader);

         

        Set<Order> orders1 = shop1.getOrder();

        for(Order order : orders1){

            System.out.println("***************************");

            System.out.println(order.getOrderNumber());

            System.out.println(order.getCustomer().getName());

            System.out.println("***************************");

        }

    }

}

  7.生成的xml文件

<?xml version="1.0"encoding="UTF-8"standalone="yes"?>

<CHMart name="CHMart">

    <number>100000</number>

    <describer>EveryThing</describer>

    <address state="China">

        <province>ZheJiang</province>

        <city>HangZhou</city>

        <street>XiHuRoad</street>

        <zip>310000</zip>

    </address>

    <orders>

        <order orderNumber="LH59800">

            <shopName>Mart</shopName>

            <price>80</price>

            <amount>1</amount>

            <purDate>2012-03-2512:57:23</purDate>

            <customer name="David">

                <gender>male</gender>

                <phoneNo>13699991000</phoneNo>

                <address state="China">

                    <province>JiangSu</province>

                    <city>NanJing</city>

                    <street>ZhongYangLu</street>

                    <zip>210000</zip>

                </address>

            </customer>

        </order>

        <order orderNumber="LH59900">

            <shopName>Mart</shopName>

            <price>60</price>

            <amount>1</amount>

            <purDate>2012-03-2512:57:23</purDate>

            <customer name="Jim">

                <gender>male</gender>

                <phoneNo>13699990000</phoneNo>

                <address state="China">

                    <province>ShangHai</province>

                    <city>ShangHai</city>

                    <street>Huang</street>

                    <zip>200000</zip>

                </address>

            </customer>

        </order>

    </orders>

</CHMart>

  以上是以一個簡單的商店訂單模型做爲示例。

相關文章
相關標籤/搜索