Java Reference
In-Depth Information
import org.springframework.batch.item.xml.StaxWriterCallback;
public class CustomerXmlHeaderCallback implements StaxWriterCallback {
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "identification"));
writer.add(factory.createStartElement("", "", "author"));
writer.add(factory.createAttribute("name", "Michael Minella"));
writer.add(factory.createEndElement("", "", "author"));
writer.add(factory.createEndElement("", "", "identification"));
} catch (XMLStreamException xmlse) {
System.err.println("An error occured: " + xmlse.getMessage());
Listing 9-69 shows CustomerXmlHeaderCallback . In the callback, you add two tags to the XML file: an
identification section and a single author section. The author section contains a single attribute called
name with the value Michael Minella . To create a tag, you use the 's
createStartElement and createEndElement methods. Each of these methods takes three parameters: a
prefix, a namespace, and the name of the tag. Because you aren't using a prefix or namespace, you pass
in empty strings for those. To put this implementation to use, you need to configure
StaxEventItemWriter to call the callback as the headerCallback . Listing 9-70 shows the configuration for
this example.
Search WWH ::

Custom Search