JAXB入门

JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式,常用的注解包括:@XmlRootElement,@XmlElement等等。

JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

mavn仓库的地址:http://mvnrepository.com/

在mavn中引入JAXB的包:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.12</version>
</dependency>

首先建一个实体类:

package com.zhcw.practice;

import sun.management.Agent;

import javax.xml.bind.annotation.*;

/**
 * Created by Administrator on 2016/12/13.
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Customer")
@XmlType(name="Customer",propOrder = {"name", "age"})
public class Customer {
    @XmlAttribute
    private int id;
    @XmlElement
    private String name;
    @XmlElement
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

然后建立测试类:

package com.zhcw.practice;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;

/**
 * Created by Administrator on 2016/12/13.
 */
public class JaxbExample {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.setId(1);
        customer.setAge(23);
        customer.setName("Hello word");
        //javaBean转为Xml
        javaBeanForXml(customer);
        //Xml转为javaBean
        File file = new File("F:\\testXMl.xml");
        XmlForJavaBean(file);
    }

    private static void javaBeanForXml(Customer customer) {
        try {
            File file = new File("F:\\testXMl.xml");
            //初始化JAXBContext.JAXBContext类提供的JAXB API的客户端的入口点。
            //它提供一个抽象的用于管理XML / Java绑定的必要信息,以实现JAXB绑定框架行动:解组,编组和验证。
            JAXBContext jc = JAXBContext.newInstance(Customer.class);
            //Java对象MarshalXML内容的Marshal的初始化设置.
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(customer, file);
            marshaller.marshal(customer, System.out);
        } catch (JAXBException e) {
            System.out.println("output xml error");
            e.printStackTrace();
        }
    }

    private static void XmlForJavaBean(File file) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Customer customer = (Customer) unmarshaller.unmarshal(file);
            System.out.println(customer.toString());
        } catch (JAXBException e) {
            System.out.println("xml for javaBean Error");
            e.printStackTrace();
        }
    }
}
控制台输出:

Connected to the target VM, address: '127.0.0.1:63217', transport: 'socket'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer id="1">
    <name>Hello word</name>
    <age>23</age>
</Customer>
Customer{id=1, name='Hello word', age=23}
Disconnected from the target VM, address: '127.0.0.1:63217', transport: 'socket'


Process finished with exit code 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值