JAXB教程-入门案例

前端之家收集整理的这篇文章主要介绍了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

   
   
   
   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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@
  • 34@H_404_41@
  • 35@H_404_41@
  • 36@H_404_41@
  • 37@H_404_41@
  • 38@H_404_41@
  • 39@H_404_41@
  • 40@H_404_41@
  • 41@H_404_41@
  • 42@H_404_41@
  • 43@H_404_41@
  • 44@H_404_41@
  • 45@H_404_41@
  • 46@H_404_41@
  • 47@H_404_41@
  • 48@H_404_41@
  • 49@H_404_41@
  • 50@H_404_41@
  • 51@H_404_41@ 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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@
  • 34@H_404_41@
  • 35@H_404_41@
  • 36@H_404_41@
  • 37@H_404_41@
  • 38@H_404_41@
  • 39@H_404_41@
  • 40@H_404_41@
  • 41@H_404_41@
  • 42@H_404_41@
  • 43@H_404_41@
  • 44@H_404_41@
  • 45@H_404_41@
  • 46@H_404_41@
  • 47@H_404_41@
  • 48@H_404_41@
  • 49@H_404_41@
  • 50@H_404_41@
  • 51@H_404_41@
  • 52@H_404_41@
  • 53@H_404_41@
  • 54@H_404_41@
  • 55@H_404_41@
  • 56@H_404_41@
  • 57@H_404_41@
  • 58@H_404_41@
  • 59@H_404_41@
  • 60@H_404_41@
  • 61@H_404_41@ /** * 收货地址 * * @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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@
  • 34@H_404_41@
  • 35@H_404_41@
  • 36@H_404_41@
  • 37@H_404_41@
  • 38@H_404_41@
  • 39@H_404_41@
  • 40@H_404_41@
  • 41@H_404_41@
  • 42@H_404_41@
  • 43@H_404_41@
  • 44@H_404_41@
  • 45@H_404_41@
  • 46@H_404_41@
  • 47@H_404_41@
  • 48@H_404_41@
  • 49@H_404_41@
  • 50@H_404_41@
  • 51@H_404_41@
  • 52@H_404_41@
  • 53@H_404_41@
  • 54@H_404_41@
  • 55@H_404_41@
  • 56@H_404_41@
  • 57@H_404_41@
  • 58@H_404_41@
  • 59@H_404_41@ 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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@
  • 34@H_404_41@
  • 35@H_404_41@
  • 36@H_404_41@
  • 37@H_404_41@
  • 38@H_404_41@
  • 39@H_404_41@
  • 40@H_404_41@ 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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@
  • 34@H_404_41@
  • 35@H_404_41@
  • 36@H_404_41@
  • 37@H_404_41@
  • 38@H_404_41@
  • 39@H_404_41@
  • 40@H_404_41@
  • 41@H_404_41@
  • 42@H_404_41@
  • 43@H_404_41@
  • 44@H_404_41@
  • 45@H_404_41@
  • 46@H_404_41@
  • 47@H_404_41@
  • 48@H_404_41@
  • 49@H_404_41@
  • 50@H_404_41@
  • 51@H_404_41@
  • 52@H_404_41@
  • 53@H_404_41@
  • 54@H_404_41@
  • 55@H_404_41@
  • 56@H_404_41@
  • 57@H_404_41@ 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@H_404_41@
  • 2@H_404_41@
  • 3@H_404_41@
  • 4@H_404_41@
  • 5@H_404_41@
  • 6@H_404_41@
  • 7@H_404_41@
  • 8@H_404_41@
  • 9@H_404_41@
  • 10@H_404_41@
  • 11@H_404_41@
  • 12@H_404_41@
  • 13@H_404_41@
  • 14@H_404_41@
  • 15@H_404_41@
  • 16@H_404_41@
  • 17@H_404_41@
  • 18@H_404_41@
  • 19@H_404_41@
  • 20@H_404_41@
  • 21@H_404_41@
  • 22@H_404_41@
  • 23@H_404_41@
  • 24@H_404_41@
  • 25@H_404_41@
  • 26@H_404_41@
  • 27@H_404_41@
  • 28@H_404_41@
  • 29@H_404_41@
  • 30@H_404_41@
  • 31@H_404_41@
  • 32@H_404_41@
  • 33@H_404_41@ 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.f2er.com/xml/294583.html

    猜你在找的XML相关文章