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,经常使⽤的只有前两个属性。如:
@XmlType(name = "basicStruct", propOrder = {
"intValue",
"stringArray",
"stringValue"
)
在使⽤@XmlType的propOrder 属性时,必须列出JavaBean对象中的所有属性,否则会报错。
2.@XmlElement
@XmlElement将java对象的属性映射为xml的节点,在使⽤@XmlElement时,可通过name属性改变java对象属性在xml中显⽰的名称。如:
@XmlElement(name="Address")
private String yourAddress;
3.@XmlRootElement
@XmlRootElement⽤于类级别的注解,对应xml的跟元素,常与 @XmlType 和 @XmlAccessorType⼀起使⽤。如:
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Address {}
4.@XmlAttribute
@XmlAttribute⽤于把java对象的属性映射为xml的属性,并可通过name属性为⽣成的xml属性指定别名。如:
@XmlAttribute(name="Country")
private String state;
5.@XmlAccessorType
@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变量上使⽤@Xml
Element和@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;
l.bind.annotation.XmlAccessOrder;
l.bind.annotation.XmlAccessType;
l.bind.annotation.XmlAccessorType;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlType;
l.bind.annotation.XmlElementWrapper;
l.bind.annotation.XmlAccessorOrder;
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)java xml是什么
@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;
l.bind.annotation.XmlAccessType;
l.bind.annotation.XmlAccessorType;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlType;
l.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.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;
l.bind.annotation.XmlType;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlAccessType;
l.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.phoneNo = phoneNo;
this.address = address;
}
getter/setter略
4.Address.java
package jaxb.shop;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlType;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
l.bind.annotation.XmlAccessType;
l.bind.annotation.XmlAccessorType;
l.bind.annotation.XmlAccessOrder;
l.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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论