Naveenkumar
Naveenkumar

Reputation: 473

Split xml file in camel using the .split().tokenizeXML()?

How to Split xml file in camel using the .split().tokenizeXML()? I have attached the code snippet. I dont know where I did mistake. Here is my input.

<Record>
  <DataFile xmlns="Created">
  </DataFile>
  <DataFile xmlns="Updated">
  </DataFile>
  <DataFile xmlns="Deleted">
  </DataFile>
</Record>

Here is my camel route

// Main Route
from(...)
.routeId("processor route")
.process(...)
.to("direct:created",
"direct:updated",
"direct:deleted").end();

// Created
from("direct:created")
.routeId("created route")
.split().tokenizeXML("xmlns:Created", "Record")
.to(...).end();

// Updated
from("direct:updated")
.routeId("updated route")
.split().tokenizeXML("xmlns:Updated", "Record")
.to(...).end();

// Deleted
from("direct:deleted")
.routeId("deleted route")
.split().tokenizeXML("xmlns:Deleted", "Record")
.to(...).end();

my expected output is ... direct:created should split and use this one only.

<DataFile xmlns="Created">
</DataFile>

direct:updated should split and use this one only.

<DataFile xmlns="Updated">
</DataFile>

and direct:deleted should split and use this one only.

<DataFile xmlns="Deleted">
</DataFile> 

Upvotes: 2

Views: 2559

Answers (2)

dey
dey

Reputation: 3140

I don't know how to get value of "xmlns" attribute in XPath, because "xmlns" is a NameSpace attribute. If you can change name of that attribute to e.g. "attribute" you can use something like this:

Firstly split xml to list of elements "DataFile", then use content-based routing using value of "attribute" ("attribute" because I don't know how to get value of "xmlns" attribute in XPath - you can find this yourself and try)

from("direct:route").split().tokenizeXML("DataFile").streaming().choice()
    .when().xpath("//DataFile[@attribute=&#39;Created&#39;]").to("direct:created")
    .when().xpath("//DataFile[@attribute=&#39;Updated&#39;]").to("direct:updated")
    .otherwise().to("direct:deleted")

Upvotes: 1

Claus Ibsen
Claus Ibsen

Reputation: 55540

You cannot split by namespace using the tokenizeXml. You would need to split the file yourself, or write some code that can split by namespace.

Upvotes: 3

Related Questions