您可以将jaxb2-maven-plugin
与jaxb-bindings
文件一起使用。
首先我创建了一个odt.xsd
文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="teste" type="Teste" />
<xsd:complexType name="Teste">
<xsd:sequence>
<xsd:element name="date" type="xsd:dateTime" minOccurs="1"
maxOccurs="1" nillable="false"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
然后我创建了一个jaxb-bindings.xjb
文件,它定义了date
字段的类型,以及从它转换到它的类:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
<jaxb:bindings schemaLocation="odt.xsd">
<jaxb:bindings node="//xsd:element[@name='date']">
<jaxb:javaType name="java.time.OffsetDateTime"
parseMethod="xsd.test.OffsetDateTimeAdapter.parse"
printMethod="xsd.test.OffsetDateTimeAdapter.print" />
</jaxb:bindings>
<jaxb:schemaBindings>
<jaxb:package name="xsd.test" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
该文件引用了将from 和 axsd.test.OffsetDateTimeAdapter
转换为的类和相应的方法,因此我也创建了它:OffsetDateTime
String
package xsd.test;
import java.time.OffsetDateTime;
public class OffsetDateTimeAdapter {
public static OffsetDateTime parse(String value) {
return OffsetDateTime.parse(value);
}
public static String print(OffsetDateTime value) {
return value.toString();
}
}
然后,在pom.xml
我添加了插件的配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- The package of your generated sources -->
<packageName>xsd.test</packageName>
<sources>
<source>src/main/resources/odt.xsd</source>
</sources>
<xjbSources>
<xjbSource>src/main/resources/jaxb-bindings.xjb</xjbSource>
</xjbSources>
</configuration>
</plugin>
有了这个,我刚刚构建了项目,mvn clean package
创建的 jar 包含包中生成的文件xsd.test
。该类Teste
包含以下date
字段OffsetDateTime
:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Teste", propOrder = {
"date"
})
public class Teste {
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected OffsetDateTime date;
// getter and setter
}
有了这个,该date
字段被映射到 a OffsetDateTime
,使用自动生成的Adapter1
(内部使用xsd.test.OffsetDateTimeAdapter
上面创建的类)。从 xml 解析日期的示例:
ObjectFactory f = new ObjectFactory();
JAXBContext context = JAXBContext.newInstance("xsd.test");
Unmarshaller unmarshaller = context.createUnmarshaller();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:teste xmlns:ns2=\"xsd.test\"><date>2016-01-01T00:00+01:00</date></ns2:teste>";
JAXBElement<Teste> jaxElement = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xml.getBytes())), Teste.class);
OffsetDateTime odt = jaxElement.getValue().getDate();
System.out.println(odt); // 2016-01-01T00:00+01:00
并且在将日期编组为 xml 时,OffsetDateTime
直接将其转换为String
诸如2016-01-01T00:00+01:00
.
另一种方法是使用xjc
JDK 自带的命令行:
xjc src/main/resources/odt.xsd -d src/main/java/ -p xsd.test -b src/main/resources/jaxb-bindings.xjb
这会src/main/java
在包的目录中生成类xsd.test
。