简介
JAXB(JavaArchitecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到 XML实例文档。
JAXB2.0是JDK 1.6的组成部分。我们不需要下载第三方jar包 即可做到轻松转换。
概念
Marshaller接口,将Java对象序列化为XML数据。
Unmarshaller接口,将XML数据反序列化为Java对象。
@XmlType,将Java类或枚举类型映射到XML模式类型
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标 注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。
@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。
@XmlRootElement,将Java类或枚举类型映射到XML元素。
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
示例
Order.java
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
package com.ricky.domain;
import javax.xml.bind.annotation.*;
import java.util.List;
import java.util.Set;
/** * 订单 * * @author Ricky Fung * @create 2016-06-13 18:27 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(propOrder = {
"id",
"totalPrice",0);
Box-sizing: border-
Box;">"category",0);
Box-sizing: border-
Box;">"shoppingList",0);
Box-sizing: border-
Box;">"tags",0);
Box-sizing: border-
Box;">"address"})
public class Order {
@XmlAttribute(name=
"id")
private long id;
private String category;
@XmlElementWrapper(name =
"shopping_list")
@XmlElement(name =
"shopping_item")
private List<ShoppingItem> shoppingList;
"tags")
"tag")
private Set<String> tags;
"addr",
required =
true)
private Address address;
"total_price")
float totalPrice;
public long getId() {
return id;
}
void setId(
long id) {
this.id = id;
}
public String
getCategory() {
return category;
}
void setCategory(String category) {
this.category = category;
}
public List<ShoppingItem>
getShoppingList() {
return shoppingList;
}
void setShoppingList(List<ShoppingItem> shoppingList) {
this.shoppingList = shoppingList;
}
public Set<String>
getTags() {
return tags;
}
void setTags(Set<String> tags) {
this.tags = tags;
}
public Address
getAddress() {
return address;
}
void setAddress(Address address) {
this.address = address;
}
float getTotalPrice() {
return totalPrice;
}
void setTotalPrice(
float totalPrice) {
this.totalPrice = totalPrice;
}
@Override
public String
toString() {
return "Order{" +
"id=" + id +
",category='" + category +
'\'' +
+ shoppingList +
+ tags +
+ address +
+ totalPrice +
'}';
}
}
ShoppingItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
/** * 购物项 * * @create 2016-06-13 19:00 */
@XmlAccessorType(XmlAccessType.FIELD)
ShoppingItem {
private String name;
float price;
int num;
public String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
float getPrice() {
return price;
}
void setPrice(float price) {
this.price = price;
}
int getNum() {
return num;
}
void setNum(int num) {
this.num = num;
}
"ShopItem{" +
"name='" + name + + price +
+ num +
'}';
}
}
Address.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
/** * 收货地址 * * @create 2016-06-13 18:28 */
Address {
private String province;
private String city;
private String district;
private String street;
public String getProvince() {
return province;
}
void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
void setStreet(String street) {
this.street = street;
}
"Address{" +
"province='" + province + + city + + district + + street + '}';
}
}
JAXBDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
package com.ricky
import .ricky.domain.Address
import .Order.JAXBUtil
import javax.xml.bind.JAXBException
import java.util.*
public class JAXBDemo {
public static void main(String[] args) throws JAXBException {
Order order = new Order()
order.setId(2)("3C")
Set<String> tags = new HashSet<String>()
tags.add("手机")(tags)
List<ShoppingItem> shopping_list = new ArrayList<ShoppingItem>()
ShoppingItem shoppingItem1 = new ShoppingItem()
shoppingItem1.setName("Apple 6s Plus 64G")(6499f)(1)
shopping_list.add(shoppingItem1)
ShoppingItem shoppingItem2 = new ShoppingItem()
shoppingItem2"魅蓝Note3 32G")f)(shoppingItem2)
order.setShoppingList(shopping_list)(7498f)
Address address = new Address()
address.setProvince("湖北省")("武汉市")("武昌区")("复兴路")(address)
String xml = JAXBUtil.beanToXml(order)
System.out.println("marshaller order:"+xml)
Order o = JAXBUtil.xmlToBean(xml,Order.class)+o)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
package com.ricky.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
/** * JAXB工具类 * * @create 2016-06-13 18:20 */
JAXBUtil {
static String beanToXml(Object obj) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(Marshaller.JAXB_ENCODING,0); Box-sizing: border-Box;">"UTF-8");
StringWriter writer = new StringWriter();
marshaller.marshal(obj,writer);
return writer.toString();
}
static <T> T xmlToBean(String xml,Class<T> cls) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(cls);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xml));
}
}
忽略字段
使用@XmlTransient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
import java.util.List;
/** * ${DESCRIPTION} * * @create 2016-06-14 18:35 */
@XmlRootElement
Student {
private String name;
@XmlTransient
int age;
"hobbies")
"hobby")
private List<String> hobbies;
int getAge() {
return age;
}
void setAge(int age) {
this.age = age;
}
public List<String> getHobbies() {
return hobbies;
}
void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
package com.ricky;
import com.ricky.domain.Student;
import com.ricky.util.JAXBUtil;
import java.util.ArrayList;
@create 2016-06-14 18:34 */
JAXBExcludeDemo {
static void main(String[] args) throws JAXBException {
Student student = new Student();
student.setId(1l);
student.setName("Ricky");
student.setAge(27);
List<String> hobbies = new ArrayList<String>();
hobbies.add("NBA");
hobbies.add("电影");
student.setHobbies(hobbies);
String xml = JAXBUtil.beanToXml(student);
System.out.println(xml);
}
}
参考资料:
https://java.net/projects/jaxb2-commons/pages/Home