微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JAXB教程-入门案例


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


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

简介

JAXBJavaArchitecture 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='" + 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.ShoppingItem.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).setCategory("3C"); Set<String> tags = new HashSet<String>(); tags.add("手机").setTags(tags); List<ShoppingItem> shopping_list = new ArrayList<ShoppingItem>(); ShoppingItem shoppingItem1 = new ShoppingItem(); shoppingItem1.setName("Apple 6s Plus 64G").setPrice(6499f).setNum(1); shopping_list.add(shoppingItem1); ShoppingItem shoppingItem2 = new ShoppingItem(); shoppingItem2"魅蓝Note3 32G")999f).add(shoppingItem2); order.setShoppingList(shopping_list).setTotalPrice(7498f); Address address = new Address(); address.setProvince("湖北省").setCity("武汉市").setdistrict("武昌区").setStreet("复兴路").setAddress(address); String xml = JAXBUtil.beanToXml(order); System.out.println("marshaller order:"+xml); Order o = JAXBUtil.xmlToBean(xml,Order.class)"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
    • 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(); // 用来指定是否使用换行和缩排对已编组XML数据进行格式化的属性名称 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

    原文地址:https://www.jb51.cc/xml/294583.html

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。