Part V. 数据访问-21. 使用O/X Mappers编组XML-21.5 JAXB

JAXB绑定编译器将W3C XML模式转换为一个或多个java类jaxb.properties文件以及可能的一些资源文件。 JAXB还提供了一种从注释java类生成模式的方法

Spring支持JAXB 2.0 API作为XML编组策略,遵循第21.2节“Marshaller和Unmarshaller”中描述的MarshallerUnmarshaller接口。 相应的集成类驻留在org.springframework.oxm.jaxb包中。

21.5.1 Jaxb2Marshaller

Jaxb2Marshaller类实现了SpringUnmarshaller接口。 它需要一个上下文路径来操作,您可以使用contextpath属性进行设置。 上下文路径是包含模式派生类的冒号(:)分离的Java包名称的列表。 它还提供了一个classesToBeBound属性,它允许您设置要由编组器支持的类的数组。 通过向bean指定一个或多个模式资源来执行模式验证,如下所示:

<beans>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>org.springframework.oxm.jaxb.Flight</value>
<value>org.springframework.oxm.jaxb.Flights</value>
</list>
</property>
<property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>
</bean>

...

</beans>

基于XML Schema的配置

jaxb2-marshaller标签配置了一个org.springframework.oxm.jaxb.Jaxb2Marshaller。 这是一个例子:

<oxm:jaxb2-marshaller id="marshaller" contextpath="org.springframework.ws.samples.airline.schema"/>

或者,可以通过待绑定的子标签将绑定的类列表提供给编组者:

"marshaller">
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/>
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Flight"/>
...
</oxm:jaxb2-marshaller>

可用属性有:

Attribute Description required
id the id of the marshaller no
contextpath the JAXB Context path no

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念