我有一个大致如下的xml文件:
<batch>
<header>
<headerStuff />
</header>
<contents>
<timestamp />
<invoices>
<invoice>
<invoiceStuff />
</invoice>
<!-- Insert 1000 invoice elements here -->
</invoices>
</contents>
</batch>
我想将该文件拆分为1000个具有相同标题的文件,并且只有一张发票。Smooks文档对转换的可能性感到非常自豪,但不幸的是,我不想这样做。
我唯一知道如何做到这一点的方法是在freemarker中重复整个结构。但这感觉像是在不必要地重复这种结构。标头有大约30个不同的标签,因此也会涉及到大量的工作。
我目前拥有的是:
<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:calc="http://www.milyn.org/xsd/smooks/calc-1.1.xsd"
xmlns:frag="http://www.milyn.org/xsd/smooks/fragment-routing-1.2.xsd"
xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
</params>
<frag:serialize fragment="INVOICE" bindTo="invoiceBean" />
<calc:counter countOnElement="INVOICE" beanId="split_calc" start="1" />
<file:outputStream openOnElement="INVOICE" resourceName="invoiceSplitStream">
<file:fileNamePattern>invoice-${split_calc}.xml</file:fileNamePattern>
<file:destinationDirectoryPattern>target/invoices</file:destinationDirectoryPattern>
<file:highWaterMark mark="10"/>
</file:outputStream>
<resource-config selector="INVOICE">
<resource>org.milyn.routing.io.OutputStreamRouter</resource>
<param name="beanId">invoiceBean</param>
<param name="resourceName">invoiceSplitStream</param>
<param name="visitAfter">true</param>
</resource-config>
</smooks-resource-list>
这为每个发票标记创建了文件,但我不知道如何继续,以便在文件中也获得标题。
编辑:
解决方案必须使用Smooks。我们在应用程序中使用它作为通用拆分器,只需为不同类型的输入文件创建不同的smooks配置文件。
我自己刚开始使用Smooks。然而你的问题听起来与此相同:http://www.smooks.org/mediawiki/index.php?title=V1.5:Smooks_v1.5_User_Guide#Routing_to_File
您必须提供完整的输出FTL格式,我想这是使用通用工具的缺点。数据映射通常包括很多感觉像冗余的东西,解决这一问题的一种方法是利用约定,但这必须构建在框架中。
我不知道smooks,但最简单的解决方案(性能较差)是(创建第N个文件):
- 复制整个xml结构
- 删除除第N个标签外的所有发票标签
我不知道如何在烟雾中做到这一点,这只是一个想法。在这种情况下,您不需要在freemarker模板中复制xml的结构。