Apache camel-按元素大小分割xml文件(每个文件的最大元素数)

问题描述 投票:0回答:1

我想分割xml文件。

原始文件:

<home>
   <address>a1</address>
   <address>a2</address>
   <address>a3</address>
   <address>a4</address>
   ....
   ....
   <address>an</address>
</home>

例如,我想分割每个文件中带有2个地址元素的xml文件。

文件1:

<home>
   <address>a1</address>
   <address>a2</address>
</home>

file2:

<home>
   <address>a1</address>
   <address>a2</address>
</home>

....

文件m:

<home>
   <address>a(n-1)</address>
   <address>an</address>
</home>

我尝试如下。但是我无法获得预期的结果。

from("file:///home/tharanga/mtx/task2/input?noop=true&delete=true")
 .split(xpath("home/address"))
 .streaming()
 .aggregate(AggregationStrategies.groupedExchange())
 .constant(true)
 .completionSize(2)
 .completionTimeout(1000)
 .to("file:///home/tharanga/mtx/task2/output2");

非常感谢您为解决此问题所提供的帮助。

java apache-camel
1个回答
0
投票

groupedExcange聚合策略会将交换合并到一个列表中,因此您必须对其进行额外的处理。为了简化起见,我建议您添加.convertBodyTo(String.class)并使用GroupedBodyAggregationStrategy而不是GroupedExchangeAggregationStrategy作为聚合的结果来获得List<String>

假设您的处理器看起来像

.process(new Processor()) {
    public void process(Exchange exchange) throws Exception {
         String body = exchange.getIn().getBody().join();
         StringBuilder sb = new StringBuilder();
         sb.append("<home>");
         sb.append(body);
         sb.append("</home>");
         exchange.getIn().setBody(sb.toString());
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.