JAXB教程-入门案例

来源:互联网 发布:中国战略布局知乎 编辑:程序博客网 时间:2024/06/08 01:16


原文地址:http://blog.csdn.net/top_code/article/details/51660191


JAXB:JavaTM Architecture for XML Binding:XML绑定Java架构

简介

JAXB(Java Architecture 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

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","category","shoppingList","tags","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;    @XmlElementWrapper(name = "tags")    @XmlElement(name = "tag")    private Set<String> tags;    @XmlElement(name = "addr", required = true)    private Address address;    @XmlElement(name = "total_price")    private float totalPrice;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getCategory() {        return category;    }    public void setCategory(String category) {        this.category = category;    }    public List<ShoppingItem> getShoppingList() {        return shoppingList;    }    public void setShoppingList(List<ShoppingItem> shoppingList) {        this.shoppingList = shoppingList;    }    public Set<String> getTags() {        return tags;    }    public void setTags(Set<String> tags) {        this.tags = tags;    }    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }    public float getTotalPrice() {        return totalPrice;    }    public void setTotalPrice(float totalPrice) {        this.totalPrice = totalPrice;    }    @Override    public String toString() {        return "Order{" +                "id=" + id +                ", category='" + category + '\'' +                ", shoppingList=" + shoppingList +                ", tags=" + tags +                ", address=" + address +                ", totalPrice=" + totalPrice +                '}';    }}
  • 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

ShoppingItem.java

package com.ricky.domain;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;/** * 购物项 * * @author Ricky Fung * @create 2016-06-13 19:00 */@XmlAccessorType(XmlAccessType.FIELD)public class ShoppingItem {    private String name;    private float price;    private int num;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public float getPrice() {        return price;    }    public void setPrice(float price) {        this.price = price;    }    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    @Override    public String toString() {        return "ShopItem{" +                "name='" + name + '\'' +                ", price=" + price +                ", num=" + num +                '}';    }}
  • 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

Address.java

package com.ricky.domain;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;/** * 收货地址 * * @author Ricky Fung * @create 2016-06-13 18:28 */@XmlAccessorType(XmlAccessType.FIELD)public class Address {    private String province;    private String city;    private String district;    private String street;    public String getProvince() {        return province;    }    public void setProvince(String province) {        this.province = province;    }    public String getCity() {        return city;    }    public void setCity(String city) {        this.city = city;    }    public String getDistrict() {        return district;    }    public void setDistrict(String district) {        this.district = district;    }    public String getStreet() {        return street;    }    public void setStreet(String street) {        this.street = street;    }    @Override    public String toString() {        return "Address{" +                "province='" + province + '\'' +                ", city='" + city + '\'' +                ", district='" + district + '\'' +                ", street='" + street + '\'' +                '}';    }}
  • 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

JAXBDemo.java

package com.ricky;import com.ricky.domain.Address;import com.ricky.domain.Order;import com.ricky.domain.ShoppingItem;import com.ricky.util.JAXBUtil;import javax.xml.bind.JAXBException;import java.util.*;/** * JAXB示例 * * @author Ricky Fung * @create 2016-06-13 18:15 */public class JAXBDemo {    public static void main(String[] args) throws JAXBException {        Order order = new Order();        order.setId(2);        order.setCategory("3C");        Set<String> tags = new HashSet<String>();        tags.add("3C");        tags.add("手机");        order.setTags(tags);        List<ShoppingItem> shopping_list = new ArrayList<ShoppingItem>();        ShoppingItem shoppingItem1 = new ShoppingItem();        shoppingItem1.setName("Apple 6s Plus 64G");        shoppingItem1.setPrice(6499f);        shoppingItem1.setNum(1);        shopping_list.add(shoppingItem1);        ShoppingItem shoppingItem2 = new ShoppingItem();        shoppingItem2.setName("魅蓝Note3 32G");        shoppingItem2.setPrice(999f);        shoppingItem2.setNum(1);        shopping_list.add(shoppingItem2);        order.setShoppingList(shopping_list);        order.setTotalPrice(7498f);        Address address = new Address();        address.setProvince("湖北省");        address.setCity("武汉市");        address.setDistrict("武昌区");        address.setStreet("复兴路");        order.setAddress(address);        String xml = JAXBUtil.beanToXml(order);        System.out.println("marshaller order:"+xml);        Order o = JAXBUtil.xmlToBean(xml, Order.class);        System.out.println("unmarshaller order:"+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
  • 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.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工具类 * * @author Ricky Fung * @create 2016-06-13 18:20 */public class JAXBUtil {    public static String beanToXml(Object obj) throws JAXBException {        JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());        Marshaller marshaller = jaxbContext.createMarshaller();        // 用来指定是否使用换行和缩排对已编组XML数据进行格式化的属性名称        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");        StringWriter writer = new StringWriter();        marshaller.marshal(obj, writer);        return writer.toString();    }    public 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));    }}
  • 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

忽略字段

使用@XmlTransient

package com.ricky.domain;import javax.xml.bind.annotation.*;import java.util.List;/** * ${DESCRIPTION} * * @author Ricky Fung * @create 2016-06-14 18:35 */@XmlAccessorType(XmlAccessType.FIELD)@XmlRootElementpublic class Student {    private long id;    private String name;    @XmlTransient    private int age;    @XmlElementWrapper(name = "hobbies")    @XmlElement(name = "hobby")    private List<String> hobbies;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public List<String> getHobbies() {        return hobbies;    }    public 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
  • 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
package com.ricky;import com.ricky.domain.Student;import com.ricky.util.JAXBUtil;import javax.xml.bind.JAXBException;import java.util.ArrayList;import java.util.List;/** * ${DESCRIPTION} * * @author Ricky Fung * @create 2016-06-14 18:34 */public class JAXBExcludeDemo {    public 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);    }}
  • 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

参考资料: 
https://java.net/projects/jaxb2-commons/pages/Home


1 0