Java Reference
In-Depth Information
for how Spring Batch handles files that already exist. However, flat files aren't the only type of file output.
XML is the other type of file output that Spring Batch provides for, and you look at it next.
StaxEventItemWriter
When you looked at reading XML back in Chapter 7, you explored how Spring Batch views XML
documents in fragments. Each of these fragments is the XML representation of a single item to be
processed. On the ItemWriter side, the same concept exists. Spring Batch generates an XML fragment for
each of the items the ItemWriter receives and writes the fragment to the file. This section looks at how
Spring Batch handles XML as an output medium.
To handle writing XML using Spring Batch, you use
org.springframework.batch.item.xml.StaxEventItemWriter . Just like the ItemReader, the Streaming API
for XML (StAX) implementation allows Spring Batch to write fragments of XML as each chunk is
processed. Just like FlatFileItemWriter , StaxEventItemWriter generates the XML a chunk at a time and
writes it to the file after the local transaction has been committed; this prevents rollback issues if there is
an error writing to the file.
The configuration of the StaxEventItemReader consists of a resource (file to read from), a root
element name (the root tag for each fragment), and an unmarshaller to be able to convert the XML input
into an object. The configuration for StaxEventItemWriter is almost identical, with a resource to write to,
a root element name (the root tag for each fragment you generate), and a marshaller to convert each
item into an XML fragment.
StaxEventItemWriter has a collection of configurable attributes that are covered in Table 9-2.
Table 9-2. Attributes Available in StaxEventItemWriter
Option
Type
Default
Description
UTF-8
encoding
String
Character encoding for the file.
footerCallback
StaxWriterCallback
null
Executed after the last item of
a file has been written.
headerCallback
StaxWriterCallback
null
Executed before the first item
of a file has been written.
marshaller
Marshaller
null
(required)
Used to convert an individual
item to an XML fragment for
output.
overwriteOutput
boolean
true
By default, the file is replaced if
the output file already exists. If
this is set to true and the file
exists, an ItemStreamException
is thrown.
File or stream to be written to.
resource
Resource
null
(required)
 
 
Search WWH ::




Custom Search