@XmlType用在class类的注解,常与@XmlRootElement,@XmlAccessorType一起使用。它有三个属性:name、propOrder、namespace,经常使用的只有前两个属性。如:
@XmlType(name = "basicStruct", propOrder = { "intValue", "stringArray", "stringValue" )
在使用@XmlType的propOrder 属性时,必须列出JavaBean对象中的所有属性,否则会报错。
@XmlElement将java对象的属性映射为xml的节点,在使用@XmlElement时,可通过name属性改变java对象属性在xml中显示的名称。如:
@XmlElement(name="Address") private String yourAddress;
@XmlRootElement用于类级别的注解,对应xml的跟元素,常与 @XmlType 和 @XmlAccessorType一起使用。如:
@XmlType @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class Address {}
@XmlAttribute(name="Country") private String state;
@XmlAccessorType用于指定由java对象生成xml文件时对java对象属性的访问方式。常与@XmlRootElement、@XmlType一起使用。它的属性值是XmlAccessType的4个枚举值,分 别为:
XmlAccessType.FIELD:java对象中的所有成员变量
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
1 import java.util.Set; 2 3 import javax.xml.bind.annotation.XmlAccessOrder; 4 import javax.xml.bind.annotation.XmlAccessType; 5 import javax.xml.bind.annotation.XmlAccessorType; 6 import javax.xml.bind.annotation.XmlAttribute; 7 import javax.xml.bind.annotation.XmlElement; 8 import javax.xml.bind.annotation.XmlRootElement; 9 import javax.xml.bind.annotation.XmlType; 10 import javax.xml.bind.annotation.XmlElementWrapper; 11 import javax.xml.bind.annotation.XmlAccessorOrder; 12 13 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL) 14 @XmlAccessorType(XmlAccessType.FIELD) 15 @XmlType(name ="shop", propOrder = {"name", "number","describer", "address","orders"}) 16 @XmlRootElement(name ="CHMart") 17 public class Shop { 18 19 @XmlAttribute 20 privateString name; 21 22 // @XmlElement 23 privateString number; 24 25 @XmlElement 26 privateString describer; 27 28 @XmlElementWrapper(name ="orders") 29 @XmlElement(name ="order") 30 privateSet<Order> orders; 31 32 @XmlElement 33 privateAddress address; 34 35 publicShop() { 36 } 37 38 publicShop(String name, String number, String describer, Address address) { 39 this.name = name; 40 this.number = number; 41 this.describer = describer; 42 this.address = address; 43 } 44 45 getter/setter略 46 }
备注:同时使用了@XmlType(propOrder={})和 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL),但是生成的xml只按照propOrder定义的顺序生成元
2.Order.java
1 import java.math.BigDecimal; 2 import java.util.Date; 3 4 import javax.xml.bind.annotation.XmlAccessType; 5 import javax.xml.bind.annotation.XmlAccessorType; 6 import javax.xml.bind.annotation.XmlAttribute; 7 import javax.xml.bind.annotation.XmlRootElement; 8 import javax.xml.bind.annotation.XmlType; 9 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 10 11 @XmlType(name="order",propOrder={"shopName","orderNumber","price","amount","purDate","customer"}) 12 @XmlAccessorType(XmlAccessType.FIELD) 13 @XmlRootElement 14 public class Order { 15 16 // @XmlElement 17 privateString shopName; 18 19 @XmlAttribute 20 privateString orderNumber; 21 22 // @XmlElement 23 @XmlJavaTypeAdapter(value=DateAdapter.class) 24 privateDate purDate; 25 26 // @XmlElement 27 privateBigDecimal price; 28 29 // @XmlElement 30 privateint amount; 31 32 // @XmlElement 33 privateCustomer customer; 34 35 publicOrder() { 36 } 37 38 publicOrder(String shopName, String orderNumber, Date purDate, 39 BigDecimal price,int amount) { 40 this.shopName = shopName; 41 this.orderNumber = orderNumber; 42 this.purDate = purDate; 43 this.price = price; 44 this.amount = amount; 45 } 46 47 getter/setter略 48 }
备注:@XmlAccessorType(XmlAccessType.FIELD),所以此处注释掉了@XmlElement,xml中依然会生成这些元素
3.Customer.java
1 import java.util.Set; 2 import javax.xml.bind.annotation.XmlType; 3 import javax.xml.bind.annotation.XmlAttribute; 4 import javax.xml.bind.annotation.XmlElement; 5 import javax.xml.bind.annotation.XmlRootElement; 6 import javax.xml.bind.annotation.XmlAccessType; 7 import javax.xml.bind.annotation.XmlAccessorType; 8 9 @XmlType 10 @XmlAccessorType(XmlAccessType.FIELD) 11 @XmlRootElement 12 public class Customer { 13 14 @XmlAttribute 15 privateString name; 16 17 privateString gender; 18 19 privateString phoneNo; 20 21 privateAddress address; 22 23 privateSet<Order> orders; 24 25 publicCustomer() { 26 } 27 28 publicCustomer(String name, String gender, String phoneNo, Address address) { 29 this.name = name; 30 this.gender = gender; 31 this.phoneNo = phoneNo; 32 this.address = address; 33 } 34 35 getter/setter略 36 }
4.Address.java
1 import javax.xml.bind.annotation.XmlAttribute; 2 import javax.xml.bind.annotation.XmlType; 3 import javax.xml.bind.annotation.XmlElement; 4 import javax.xml.bind.annotation.XmlRootElement; 5 import javax.xml.bind.annotation.XmlAccessType; 6 import javax.xml.bind.annotation.XmlAccessorType; 7 import javax.xml.bind.annotation.XmlAccessOrder; 8 import javax.xml.bind.annotation.XmlAccessorOrder; 9 10 @XmlType(propOrder={"state","province","city","street","zip"}) 11 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL) 12 @XmlAccessorType(XmlAccessType.NONE) 13 @XmlRootElement 14 public class Address { 15 16 @XmlAttribute 17 privateString state; 18 19 @XmlElement 20 privateString province; 21 22 @XmlElement 23 privateString city; 24 25 @XmlElement 26 privateString street; 27 28 @XmlElement 29 privateString zip; 30 31 publicAddress() { 32 super(); 33 } 34 35 publicAddress(String state, String province, String city, String street, 36 String zip) { 37 super(); 38 this.state = state; 39 this.province = province; 40 this.city = city; 41 this.street = street; 42 this.zip = zip; 43 } 44 45 getter/setter略 46 }
备注:虽然@XmlAccessorType为XmlAccessType.NONE,但是在java类的私有属性上加了@XmlAttribute和@XmlElement注解后,这些私有成员会映射生成xml的元素
5.DateAdapter.java
1 import java.util.Date; 2 import java.text.SimpleDateFormat; 3 4 import javax.xml.bind.annotation.adapters.XmlAdapter; 5 6 public class DateAdapter extendsXmlAdapter<String, Date> { 7 8 private String pattern = "yyyy-MM-dd HH:mm:ss"; 9 SimpleDateFormat fmt =new SimpleDateFormat(pattern); 10 11 @Override 12 publicDate unmarshal(String dateStr) throwsException { 13 return fmt.parse(dateStr); 14 } 15 16 @Override 17 publicString marshal(Date date) throwsException { 18 return fmt.format(date); 19 } 20 21 }
备注:用于格式化日期在xml中的显示格式,并且由xml unmarshal为java对象时,将字符串解析为Date对象
6.ShopTest.java
1 import java.io.FileReader; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 import java.math.BigDecimal; 5 import java.util.Date; 6 import java.util.HashSet; 7 import java.util.Set; 8 9 import javax.xml.bind.JAXBContext; 10 import javax.xml.bind.JAXBException; 11 import javax.xml.bind.Marshaller; 12 import javax.xml.bind.Unmarshaller; 13 14 public class ShopTest { 15 16 publicstatic void main(String[] args) throws JAXBException, IOException{ 17 Set<Order> orders =new HashSet<Order>(); 18 19 Address address1 =new Address("China","ShangHai", "ShangHai", "Huang","200000"); 20 Customer customer1 =new Customer("Jim","male", "13699990000", address1); 21 Order order1 =new Order("Mart","LH59900", new Date(), newBigDecimal(60),1); 22 order1.setCustomer(customer1); 23 24 Address address2 =new Address("China","JiangSu", "NanJing", "ZhongYangLu","210000"); 25 Customer customer2 =new Customer("David","male", "13699991000", address2); 26 Order order2 =new Order("Mart","LH59800", new Date(), newBigDecimal(80),1); 27 order2.setCustomer(customer2); 28 29 orders.add(order1); 30 orders.add(order2); 31 32 Address address3 =new Address("China","ZheJiang", "HangZhou", "XiHuRoad","310000"); 33 Shop shop =new Shop("CHMart","100000", "EveryThing",address3); 34 shop.setOrder(orders); 35 36 37 FileWriter writer =null; 38 JAXBContext context = JAXBContext.newInstance(Shop.class); 39 try{ 40 Marshaller marshal = context.createMarshaller(); 41 marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); 42 marshal.marshal(shop, System.out); 43 44 writer =new FileWriter("shop.xml"); 45 marshal.marshal(shop, writer); 46 }catch (Exception e) { 47 e.printStackTrace(); 48 } 49 50 Unmarshaller unmarshal = context.createUnmarshaller(); 51 FileReader reader =new FileReader("shop.xml") ; 52 Shop shop1 = (Shop)unmarshal.unmarshal(reader); 53 54 Set<Order> orders1 = shop1.getOrder(); 55 for(Order order : orders1){ 56 System.out.println("***************************"); 57 System.out.println(order.getOrderNumber()); 58 System.out.println(order.getCustomer().getName()); 59 System.out.println("***************************"); 60 } 61 } 62 }
7.生成的xml文件
1 <?xml version="1.0"encoding="UTF-8"standalone="yes"?> 2 <CHMart name="CHMart"> 3 <number>100000</number> 4 <describer>EveryThing</describer> 5 <address state="China"> 6 <province>ZheJiang</province> 7 <city>HangZhou</city> 8 <street>XiHuRoad</street> 9 <zip>310000</zip> 10 </address> 11 <orders> 12 <order orderNumber="LH59800"> 13 <shopName>Mart</shopName> 14 <price>80</price> 15 <amount>1</amount> 16 <purDate>2012-03-2512:57:23</purDate> 17 <customer name="David"> 18 <gender>male</gender> 19 <phoneNo>13699991000</phoneNo> 20 <address state="China"> 21 <province>JiangSu</province> 22 <city>NanJing</city> 23 <street>ZhongYangLu</street> 24 <zip>210000</zip> 25 </address> 26 </customer> 27 </order> 28 <order orderNumber="LH59900"> 29 <shopName>Mart</shopName> 30 <price>60</price> 31 <amount>1</amount> 32 <purDate>2012-03-2512:57:23</purDate> 33 <customer name="Jim"> 34 <gender>male</gender> 35 <phoneNo>13699990000</phoneNo> 36 <address state="China"> 37 <province>ShangHai</province> 38 <city>ShangHai</city> 39 <street>Huang</street> 40 <zip>200000</zip> 41 </address> 42 </customer> 43 </order> 44 </orders> 45 </CHMart>
以上是以一个简单的商店订单模型作为示例。
转自:http://blog.csdn.net/czplplp_900725/article/details/7888896