In Camel how can i split a route using xpath on a header

女生的网名这么多〃 提交于 2019-12-11 18:19:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!