這有可能聚合多個小型XML文檔:Apache的駱駝的Aggregator2 XML文檔合併爲一個巨大的文件
<doc><field name="XXX">fieldValue</field><doc>
使用aggregator2(駱駝2.7.0)組合成一個大文件
<result><doc>...</doc><doc>...</doc><doc>...</doc>...<doc>...</doc></result>
沒有使用一些自定義聚合器處理器?我已經設法完成了創建自定義聚合器的工作,但現在我正在簡化我的代碼,所以如果駱駝支持開箱即用,那麼我們就可以擺脫它。
我的自定義聚合的樣子:
class DocsAggregator implements Processor {
void process(Exchange exchange) {
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def Document parentDoc = builder.parse(new ByteArrayInputStream("<?xml version='1.0'?><add></add>".toString().bytes));
def groupedExchanges = exchange.properties.find {it.key == 'CamelGroupedExchange'}
groupedExchanges.value.each { Exchange x ->
def Document document = x.'in'.body
def bos = new ByteArrayOutputStream()
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(bos))
def node = document.documentElement.childNodes.find { Node it -> it.nodeType == Node.ELEMENT_NODE}
def cloned = parentDoc.adoptNode(node)
parentDoc.documentElement.appendChild(cloned)
}
exchange.in.body = parentDoc
}
}
感謝您的建議。這個解決方案真的看起來更好 – Archer 2011-04-29 09:31:55
我一直在用這個作爲我自己的(第一次)聚合XML文檔的指導。不幸的是,它看起來像TypeConverter不想轉換我的XML,而是返回一個空對象。將類型更改爲'String.class'確實打印完整的文檔。 – Lilienthal 2013-05-06 11:42:39