@XmlType用在class類的註解,常與@XmlRootElement,@XmlAccessorType一塊兒使用。它有三個屬性:name、propOrder、namespace,常用的只有前兩個屬性。如:html
@XmlType(name = "basicStruct", propOrder = { "intValue", "stringArray", "stringValue" )
在使用@XmlType的propOrder 屬性時,必須列出JavaBean對象中的全部屬性,不然會報錯。
@XmlElement將Java對象的屬性映射爲xml的節點,在使用@XmlElement時,可經過name屬性改變java對象屬性在xml中顯示的名稱。如:java
@XmlElement(name="Address") app
private String yourAddress;ide
@XmlRootElement用於類級別的註解,對應xml的跟元素,常與 @XmlType 和 @XmlAccessorType一塊兒使用。如:
this
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Address {}spa
@XmlAttribute用於把java對象的屬性映射爲xml的屬性,並可經過name屬性爲生成的xml屬性指定別名。如:.net
@XmlAttribute(name="Country")
private String state;code
@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文件。
@XmlAccessorOrder用於對java對象生成的xml元素進行排序。它有兩個屬性值:
AccessorOrder.ALPHABETICAL:對生成的xml元素按字母書序排序
XmlAccessOrder.UNDEFINED:不排序
@XmlTransient用於標示在由java對象映射xml時,忽略此屬性。即,在生成的xml文件中不出現此元素。
@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
|
|
|
2.Order.java
|
|
|
3.Customer.java
|
|
4.Address.java
|
|
|
5.DateAdapter.java
|
|
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>
以上是以一個簡單的商店訂單模型做爲示例。