Java Reference
In-Depth Information
To parse your XML input file, you will use the
org.springframework.batch.item.xml.StaxEventItemReader that Spring Batch provides. To use it, you
define a fragment root element name, which identifies the root element of each fragment considered an
item in your XML. In your case, this will be the customer tag. It also takes a resource, which will be the
same your customerFile bean as it has been previously. Finally, it takes an
org.springframework.oxm.Unmarshaller implementation. This will be used to convert the XML to your
domain object. Listing 7-35 shows the configuration of your customerFileReader using the
StaxEventItemReader implementation.
Listing 7-35. customerFileReader Configured with the StaxEventItemReader
<beans:bean id="customerFile"
class="org.springframework.core.io.FileSystemResource" scope="step">
<beans:constructor-arg value="#{jobParameters[customerFile]}"/>
</beans:bean>
<beans:bean id="customerFileReader"
class="org.springframework.batch.item.xml.StaxEventItemReader">
<beans:property name="fragmentRootElementName" value="customer" />
<beans:property name="resource" ref="customerFile" />
<beans:property name="unmarshaller" ref="customerMarshaller" />
</beans:bean>
Spring Batch is not picky about the XML binding technology you choose to use. Spring provides
Unmarshaller implementations that use Castor, JAXB, JiBX, XMLBeans, and XStream in their oxm
package. For this example, you will use the XStream binding framework.
For your customerMarshaller configuration, you will use the
org.springframework.oxm.xstream.XStreamMarshaller implementation provided by Spring. To parse
your customer file, there are three things you will need to configure on the XStreamMarshaller instance.
1. Aliases : This is a map of tag names to fully qualified class names that tells the
unmarshaller what each tag maps to.
2. implicitCollection : This is a map of fields to fully qualified classes that indicate
what fields on the class specified are collections consisting of another type.
3. Converters : Although XStream is pretty smart and can figure out how to
convert most of your XML file from the Strings it sees in the file to the required
data type in your objects, you need to help it out on the transaction date. For
XStream to be able to parse the transaction date, you will need to provide a
DateConverter instance configured with the correct date format.
Listing 7-36 shows how to configure your XStreamMarshaller with these dependencies.
Listing 7-36. customerMarshaller Configuration
<beans:bean id="customerMarshaller"
class="org.springframework.oxm.xstream.XStreamMarshaller">
<beans:property name="aliases">
<beans:map>
 
Search WWH ::




Custom Search