简介
JAXB(Java Architecture for XML Binding)就是XML数据绑定的java架构。JAXB可以根据XML Schema生成java类,也能根据java类生成XML Schema,XML数据绑定指定了XML请求和XML响应如何映射成java对象。
JAXB提供了API和工具,可以自动在XML文档和java对象之间映射。
JAXB框架可以使开发者进行如下操作:
- 将XML内容解包为java表示。
- 访问和更新java表示。
- 将java表示的XML内容打包回XML内容。
JAXB为开发者提供了一个高效、标准的方式来映射XML和java代码。java开发者的效率可以更高,因为他们可以写更少的代码,而且不必是XML专家。JAXB使开发者结合XML和Web服务技术更加容易扩展他们的应用。
各版本参考资源
JAXB 2.0版本参考资源
https://jcp.org/en/jsr/detail?id=222
JAXB 2.3版本详情页:
https://jcp.org/aboutJava/communityprocess/mrel/jsr222/index3.html
从该页面可以下载规范文档、和javadoc文档:
JAXB 2.3版本规范:
https://download.oracle.com/otn-pub/jcp/jaxb-2_3-mrel3-eval-spec/JAXB-2.3-spec.pdf
JAXB 3.0版本参考资源
https://jakarta.ee/specifications/xml-binding/3.0/
JAXB 4.0版本参考资源
https://jakarta.ee/specifications/xml-binding/4.0/
JAXB参考实现
旧版本(已经归档,只读)
https://github.com/javaee/jaxb-v2
这个项目库2019年2月份已经归档了,现在只读。新的项目这已经转移到https://github.com/eclipse-ee4j/jaxb-ri
新版本
代码库:https://github.com/eclipse-ee4j/jaxb-ri
主页:https://eclipse-ee4j.github.io/jaxb-ri/
下载zip独立发行包版本
打开主页:https://eclipse-ee4j.github.io/jaxb-ri/
点击右边的Download,可以下载最新的版本,例如jaxb-ri-4.0.3.zip:
将下载下来的压缩包解压,目录结构为:
bin子目录下的内容:
docs子目录下的内容:
mod子目录下的内容:
samples子目录下的内容:
对JDK版本的要求
参考实现4.0.3版本需要Java SE 11及以上。
利用运行时绑定框架
Schema到Java:开发应用的基本步骤
Schema转Java是将1个或多个schema文件编译为生成java类的过程。用这种方式开发一个应用的基本步骤如下:
- 开发或者定位到schema
- 如果需要的话,用绑定定制化注解schema,或者将它们放在一个外部的绑定文件
- 用xjc绑定编译器编译schema,生成java内容类
- 用上面生成的java内容类和jakarta.xml.bind运行时框架来开发JAXB应用
- 设置CLASSPATH变量,将下面的jar文件包含到路径中
https://eclipse-ee4j.github.io/jaxb-ri/4.0.3/docs/ch02.html#jars
上面的jar文件在下载包解压后的mod子目录下可以找到:
- 用javac编译所有的java源文件
- 运行程序
Java到Schema:开发应用的基本步骤
Java到Schema是利用jakarta.xml.bind.annotation包中的注解来增强已经存在的java类的过程,由此JAXB运行时绑定框架可以进行解包/打包的操作。用这种方式开发一个应用的基本步骤如下:
- 用java开发数据模型
- 利用jakarta.xml.bind.annotation包中的注解来控制绑定过程
- 设置CLASSPATH变量,将下面的jar文件包含到路径中
https://eclipse-ee4j.github.io/jaxb-ri/4.0.3/docs/ch02.html#jars
- 用javac编译数据模型
- 编译生成的class文件会包含自己手动增加的注解和JAXB绑定框架需要的其它默认注解
- 用数据模型开发应用;编写代码,用JAXB运行时框架的打包/解包操作持久化数据模型
- 编译、运行应用
xjc(根据xml schema生成java代码)
备注:xjc命令行脚本只存在于JAXB的zip独立发行包版本中。
拉起xjc的方式
以在Windows下使用为例,可以运行bin目录下的批处理文件xjc.bat、或者mod目录下的jaxb-xjc.jar文件来拉起xjc。
xjc.bat文件在解压包的bin子目录下:
jaxb-xjc.jar文件在解压包的mod子目录下:
运行xjc.bat文件,例如:
运行jaxb-xjc.jar文件,例如:
xjc语法
可以通过运行xjc.bat文件、或者jaxb-xjc.jar文件,后面加上-help参数,来查看具体的用法,有哪些选项。
也可以查询官网来了解具体用法:
https://eclipse-ee4j.github.io/jaxb-ri/4.0.3/docs/ch04.html#tools-xjc
xjc [OPTION]... <schema file/URL/dir/jar> [-b <binding>...]
举例
根据一个schema文件生成java文件
例如,在cmd窗口下执行如下命令:
xjc D:\temp\xmlschema\test.xsd -d D:\temp\outcode
其中:
D:\temp\xmlschema\test.xsd指定schema文件
-d D:\temp\outcode指定生成的java文件存放目录。注意,输出目录必须已经存在,xjc编译器不会创建。
schema文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: BSD-3-Clause
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/xml.xsd"/>
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root" type="foo"/>
</xs:schema>
到输出目录下查看生成的内容:
其中generated是默认生成的包名。
生成的Foo.java文件的内容:
//
// 此文件是由 Eclipse Implementation of JAXB v4.0.3 生成的
// 请访问 https://eclipse-ee4j.github.io/jaxb-ri
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
//
package generated;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlType;
/**
* <p>foo complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>{@code
* <complexType name="foo">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="age" type="{http://www.w3.org/2001/XMLSchema}int"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* }</pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
"age"
})
public class Foo {
protected int age;
/**
* 获取age属性的值。
*
*/
public int getAge() {
return age;
}
/**
* 设置age属性的值。
*
*/
public void setAge(int value) {
this.age = value;
}
}
生成的ObjectFactory.java文件的内容:
//
// 此文件是由 Eclipse Implementation of JAXB v4.0.3 生成的
// 请访问 https://eclipse-ee4j.github.io/jaxb-ri
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
//
package generated;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.annotation.XmlElementDecl;
import jakarta.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programmatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private static final QName _Root_QNAME = new QName("", "root");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Foo }
*
* @return
* the new instance of {@link Foo }
*/
public Foo createFoo() {
return new Foo();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Foo }{@code >}
*
* @param value
* Java instance representing xml element's value.
* @return
* the new instance of {@link JAXBElement }{@code <}{@link Foo }{@code >}
*/
@XmlElementDecl(namespace = "", name = "root")
public JAXBElement<Foo> createRoot(Foo value) {
return new JAXBElement<>(_Root_QNAME, Foo.class, null, value);
}
}
schemagen(根据java文件生成schema)
备注:schemagen命令行脚本只存在于JAXB的zip独立发行包版本中。
介绍
schemagen可以处理java源文件、或者class文件。
拉起schemagen的方式
以windows为例,可以运行bin目录下的schemagen.bat文件来拉起schemagen。
如果java源文件或者类文件引用了其它的类,那么被引用的类必须通过系统环境变量CLASSPATH可以访问到,否则就必须在工具命令中通过-classpath/ -cp可选参数来指定。
例如:
schemagen语法
介绍
schemagen [-options ...] <java files>
可以通过官网查看命令参数详细介绍:
https://eclipse-ee4j.github.io/jaxb-ri/4.0.3/docs/ch04.html#tools-schemagen
或者执行schemagen -help命令查看帮助信息:
查看帮助信息
执行命令:
schemagen -help
查看版本信息
执行命令:
schemagen -version
查看完整版本信息
执行命令:
schemagen -fullversion
举例
根据java源文件生成schema文件
java源文件的内容如下。其中属性name用了JavaBeans样式(get和set),属性age用了JAXB的注释:
package com.thb;
import jakarta.xml.bind.annotation.XmlElement;
public class Demo {
private String name;
@XmlElement
private int age;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
执行命令及输出:
schemagen D:\temp\eclipse-workspace\java_work\java_test2\src\com\thb\Demo.java -d D:\temp\outschema
其中 -d D:\temp\outschema指定了生成的schema文件、class文件的存放路径。
到outschema目录下查看生成的内容:
可以看到,生成了schema文件schema1.xsd,另外,也将java源文件编译成了class文件。
打开生成的schema1.xsd文件,内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="demo">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
通过maven下载JAXB的Eclipse实现
在maven工程的pom.xml文件中依赖部分增加如下片段:
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.3</version>
</dependency>
</dependencies>