user4022167
user4022167

Reputation:

Apache Camel Splitter doesn't split correctly xml file

I have a big xml file and wanted to split it in small individual files with the Apache Camel Splitter. Therefore I used the code of the example:

<route>
<from uri="file:inbox"/>
<split streaming="true>
<tokenize token="order" xml="true"/>
<to uri="activemq:queue:order"/>
</split>
</route>

my xml file looks so:

<orders>
<order>
<Parameter Name="CustomerID" Value="701423"/>
</order>
<order>
<Parameter Name="CustomerID" Value="7011337"/>
</order>
<order>
<Parameter Name="CustomerID" Value="701789"/>
</order>
</orders>

At the end my splitted message looks like the followings:

<order>
<Parameter Name="CustomerID" Value="701789"/>
</order>

I've just only one file with the last order of the big xml file instead of many small xml files. Could you tell me what is wrong? Thank you so much!

Upvotes: 1

Views: 2783

Answers (4)

Portylon
Portylon

Reputation: 11

The splitter EIP pattern generates one new route for each element splitted, if you check the JMS queue (to uri="activemq:queue:order"), you will have a message for each splitted element. In addition, for each iteration in the splitter your body is replaced with the current splitted element, in this form, when finish the splitter your body will have the last splitted element.

To get a body with all splitted elements at finish, you need an aggregator EIP pattern, that joins all splitted elements.

To get the original body when the splitter finish, you can saved it previously in a exchange property and restore from it when finish the splitter.

Upvotes: 1

Dorian
Dorian

Reputation: 1

Could be, that the result files will overwrite each other. Then this should work:

<route>
    <from uri="file:inbox" />
        <split streaming="true>
            <tokenize token=" order " xml="true" />
            <to uri="file:outbox?fileName=${file:name.noext}-${exchangeId}.${file:name.ext}" />
        </split>
</route>

Upvotes: 0

Rajesh Dommati
Rajesh Dommati

Reputation: 21

Please try the inheritNamespaceTagName option for xml mode as below

<tokenize token="order" inheritNamespaceTagName="orders" xml="true"/>

Upvotes: 0

Peter Keller
Peter Keller

Reputation: 7646

Your route works correctly for me.

Are you absolutely sure, that this is the XML you are parsing? Or if it is another one, did you check if it is valid (all open and closing tags etc.)?

Perhaps you may log the body before splitting:

<log message="body: ${body}"/>

EDIT:

I tested with following route:

<route>
    <from uri="direct:start" />
    <log message="body: ${body}"/>
    <split streaming="true">
        <tokenize token="order" xml="true"/>
        <log message="split: ${body}"/>
    </split>
</route>

Invoking the route:

String XML = "<orders><order><Parameter Name=\"CustomerID\" Value=\"701423\"/></order><order><Parameter Name=\"CustomerID\" Value=\"7011337\"/></order><order><Parameter Name=\"CustomerID\" Value=\"701789\"/></order></orders>";
ProducerTemplate template = main.getCamelTemplate();
template.sendBody("direct:start", XML);

This prints following output:

INFO  body: <orders><order><Parameter Name="CustomerID" Value="701423"/></order><order><Parameter Name="CustomerID" Value="7011337"/></order><order><Parameter Name="CustomerID" Value="701789"/></order></orders>
INFO  split: <order><Parameter Name="CustomerID" Value="701423"/></order>
INFO  split: <order><Parameter Name="CustomerID" Value="7011337"/></order>
INFO  split: <order><Parameter Name="CustomerID" Value="701789"/></order>

Upvotes: 1

Related Questions