问题
As the title suggests, i have a route with a header that contains some xml similar to the following snippet as a string;
<files>
<image_file1>image.png</image_file1>
<image_file2>image.png</image_file2>
</files>
What I'm trying do do is split via xpath using something like the following. As the following suggests when the xml is part of the body, all runs fine:-
from(myIncomingQueue)
.convertBodyTo(String.class, "utf-8")
.split(xpath("//*[local-name()='files']/*"))
.setHeader("FilePropertyToRetrieve", xpath("local-name(//*)").stringResult())
.to(myFileDownloadQueue)
.routeId("COMMON-CON-Attachment_Router-Id");
I found a solution using the following:-
from(myIncomingQueue)
.setBody(header("myHeaderWithXml"))
.convertBodyTo(String.class, "utf-8")
.split(xpath("//*[local-name()='files']/*"))
.setHeader("FilePropertyToRetrieve", xpath("local-name(//*)").stringResult())
.setBody(header("CamelOriginalBody"))
.to(myFileDownloadQueue)
.routeId("COMMON-CON-Attachment_Router-Id");
But would still like to know for learning purposes, if there is a way to do it without moving the header into the body and then reversing afterwards?
回答1:
Yes if you read the docs about xpath at: http://camel.apache.org/xpath - then see the section Using XPath on Headers
Something alike
.split(xpath("//*[local-name()='files']/*", "myHeaderWithXml"))
来源:https://stackoverflow.com/questions/18483320/in-camel-how-can-i-split-a-route-using-xpath-on-a-header