JAXB 笔记

一、简介

JAXB(Java Architecture for XML Binding简称JAXB)允许Java开发人员将Java类映射为XML表示方式。JAXB提供两种主要特性:将一个Java对象序列化为XML,以及反向操作,将XML解析成Java对象。换句话说,JAXB允许以XML格式存储和读取数据,而不需要程序的类结构实现特定的读取XML和保存XML的代码!

JAXB 原本是 J2EE 的一部分,从 JDK6 开始,JAXB 已经加入到 J2SE 中,以下为目前各个版本 JDK 中 JAXB 版本:

  • J2SE 6 : JAXB 2.0 (JSR 222)
  • J2SE 7 : JAXB 2.2.3 (JSR 222, maintenance release 2)
  • J2SE 8 : JAXB 2.2.8

二、JAXB 与 DOM/SAX/JDOM/DOM4J 等比较

JAXB 致力于解决 XML 与 Java 之间的对象映射关系,而 DOM/SAX 等是解析 XML 的底层 API;从这里可以看出两者出发点有着本质不同,JAXB 底层也会调用 SAX 来解析 XML 文件。单纯的解析 XML 文件时,我们要考虑使用 DOM/SAX 等解析技术,而涉及到 XML 到 Java 对象映射时,我们应当考虑使用 JAXB 实现。

三、Object2XML 编组(marshal)

从 Java 对象转化到 XML 文件的过程我们称之为 **编组(malshal)**;以下为代码示例:

Persion 对象 带转化的 Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package me.mritd.test;

import javax.xml.bind.annotation.XmlRootElement;

/**
* Created by mritd on 16/4/7.
*/

// JAXB 注解,标注根节点名称
@XmlRootElement(name = "Persion")
public class Persion {

private String name;

private String address;

private int age;

// 省略 SET GET 方法
}

编组(Marshal)测试类

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
public class JaxbMarshalTest {

public static void main(String[] args) {

// 首先 创建一个 待编组的 Object
Persion persion = new Persion("name","beijing",10);

try {
// 初始化 marshal
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Marshaller marshaller = jaxbContext.createMarshaller();

// 设置格式化输出 xml
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

// 执行编组
marshaller.marshal(persion,new File("Persion.xml"));

// 输出到控制台
marshaller.marshal(persion,System.out);

} catch (JAXBException e) {
e.printStackTrace();
}
}

四、Object2XML 解组/反编组(UnMarshal)

从已有的 XML 文件转化为 Object 对象的过程我们称之为解组/反编组(UnMarshal),以下伪代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class JaxbUnMarshalTest {

public static void main(String[] args) {

// 实例化文件
File file = new File("Persion.xml");

try {
// 创建 UnMarshaller
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

// 解组 操作
Persion persion = (Persion) unmarshaller.unmarshal(file);
System.out.println(persion);
} catch (JAXBException e) {
e.printStackTrace();
}
}

}

五、适配器(Adapters)

在普通的 POJO 与 XML 互相转化时,简单的编组和解组已经完全可以满足需要,但是当包含复杂类型的 POJO 等情况出现时,组需要使用适配器加以辅助编组与解组,样例代码如下:

首先定义的 Persion 中有一属性为 LocalDate 类型,此类型为复杂类型,需要定义适配器:

LocalDate Adapter

1
2
3
4
5
6
7
8
9
10
11
12
13
public class DateAdapter extends XmlAdapter<String, LocalDate> {


@Override
public LocalDate unmarshal(String v) throws Exception {
return LocalDate.parse(v);
}

@Override
public String marshal(LocalDate v) throws Exception {
return v.toString();
}
}

POJO 如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// JAXB 注解,标注根节点名称
@XmlRootElement(name = "Persion")
public class Persion {

private String name;
private String address;
private int age;
private LocalDate date;

// 自定义适配器
@XmlJavaTypeAdapter(DateAdapter.class)
public LocalDate getDate() {
return date;
}

// 省略其他 SET GET 方法
}

测试类

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
public class PersionMarshalAndUnMarshal {


public static void main(String[] args) {
// 创建对象
Persion persion = new Persion("mritd","beijing",18,LocalDate.of(2016,4,8));

try {

// 创建 marshaller
JAXBContext jaxbContext = JAXBContext.newInstance(Persion.class);
Marshaller marshaller = jaxbContext.createMarshaller();

// 创建编组文件
File file = new File("Persion.xml");

// 执行编组
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(persion,file);
marshaller.marshal(persion,System.out);

// 创建 unMarshaller
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Persion unPersion = (Persion)unmarshaller.unmarshal(file);
System.out.println(unPersion);
} catch (JAXBException e) {
e.printStackTrace();
}
}

}

六、其他参考


JAXB 笔记
https://mritd.com/2016/04/09/jaxb-note/
作者
Kovacs
发布于
2016年4月9日
许可协议