[英]How to split xml to header and items using smooks?
我有一個大致像這樣的xml文件:
<batch>
<header>
<headerStuff />
</header>
<contents>
<timestamp />
<invoices>
<invoice>
<invoiceStuff />
</invoice>
<!-- Insert 1000 invoice elements here -->
</invoices>
</contents>
</batch>
我想使用相同的headerStuff和僅一張發票將該文件拆分為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格式,這是使用通用工具的缺點。 數據映射通常包括許多感覺上的冗余,一種解決方法是利用約定,但是必須將其內置到框架中。
我不知道欺騙,但最簡單的解決方案(性能較差)是(創建第N個文件):
我不知道該怎么做,這只是一個主意。 在這種情況下,您不需要在freemarker模板中復制xml的結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.