我能够使用带有根标记的StaxEventItemWriter从数据库读取数据并编写XML。如何在XML文件中添加页眉和页脚?
@Bean(destroyMethod="")
ItemWriter<BatchRequest> databaseXmlItemWriter() {
StaxEventItemWriter<BatchRequest> xmlFileWriter = new StaxEventItemWriter<>();
xmlFileWriter.setResource(new FileSystemResource(System.getProperty("outputFile")));
xmlFileWriter.setRootTagName("employee");
Jaxb2Marshaller employeeMarshaller = new Jaxb2Marshaller();
employeeMarshaller.setClassesToBeBound(BatchRequest.class);
xmlFileWriter.setMarshaller(employeeMarshaller);
return xmlFileWriter;
}
答案 0 :(得分:1)
您可以使用org.springframework.batch.item.xml.StaxEventItemWriter#setHeaderCallback
和org.springframework.batch.item.xml.StaxEventItemWriter#setFooterCallback
通过org.springframework.batch.item.xml.StaxWriterCallback
将标题/页脚写入XML文件。
答案 1 :(得分:0)
正如@Mahmoud 在他的回答中所说,我们可以使用 StaxEventItemWriter's
setHeaderCallback()
和 setFooterCallback()
来注册我们的页眉和页脚代码。
以下是给您的代码片段:
@StepScope
@Bean(name = "staxTransactionWriter")
public StaxEventItemWriter<BatchRequest> staxTransactionItemWriter(@Value("#{jobParameters['file.path']}") String path) {
Resource exportFileResource = new FileSystemResource(path);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setSupportJaxbElementClass(true);
marshaller.setClassesToBeBound(BatchRequest.class);
StaxEventItemReaderBuilder<BatchRequest> writer = new StaxEventItemReaderBuilder<BatchRequest>();
writer.setName("employeeWriter");
writer.setVersion("1.0");
writer.setResource(exportFileResource);
writer.setMarshaller(marshaller);
writer.setRootTagName("employee");
writer.setHeaderCallback(getXmlHeaderCallBack());
writer.setFooterCallback(getXmlFooterCallBack());
writer.setShouldDeleteIfEmpty(true);
return writer;
}
@Bean
OmegaXmlHeaderCallBack getXmlHeaderCallBack(){
return new XmlHeaderCallBack();
}
@Bean
OmegaXmlFooterCallBack getXmlFooterCallBack(){
return new XmlFooterCallBack();
}
下面的代码创建了一个自定义的页眉和页脚类,它扩展了 StaxWriterCallback:
@Slf4j
public class XmlFooterCallBack implements StaxWriterCallback {
@Override
public void write(XMLEventWriter writer) {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createEndElement("", "", "record"));
} catch (XMLStreamException e) {
log.error("Error writing OMEGA XML Footer: {}", e.getMessage());
throw new OmegaXmlHeaderWriterException(e.getMessage());
}
}
}
@Slf4j
public class XmlHeaderCallBack implements StaxWriterCallback {
public XmlHeaderCallBack() {
super();
}
@Override
public void write(XMLEventWriter writer) {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "fileInformation"));
writer.add(factory.createStartElement("", "", "sender"));
writer.add(factory.createCharacters("SENDER ID"));
writer.add(factory.createEndElement("", "", "sender"));
writer.add(factory.createStartElement("", "", "timestamp"));
writer.add(factory.createCharacters("CURRENT DATE_TIME"));
writer.add(factory.createEndElement("", "", "timestamp"));
writer.add(factory.createStartElement("", "", "environment"));
writer.add(factory.createCharacters("LOCAL"));
writer.add(factory.createEndElement("", "", "environment"));
writer.add(factory.createStartElement("", "", "version"));
writer.add(factory.createCharacters("1.2.3"));
writer.add(factory.createEndElement("", "", "version"));
writer.add(factory.createEndElement("", "", "fileInformation"));
writer.add(factory.createStartElement("", "", "record"));
} catch (XMLStreamException e) {
log.error("Error writing XML Header: {}", e.getMessage());
throw new CustomXmlHeaderWriterException(e.getMessage());
}
}
}
<块引用>
标题看起来像
<fileInformation>
<sender>SENDER ID</sender>
<timestamp>CURRENT DATE_TIME</timestamp>
<environment>LOCAL</environment>
<version>1.2.3</version>
</fileInformation>
<record>
<块引用>
页脚的结束标记为 record
</record>