带有节点列表的Apache Camel XPath [英] Apache Camel XPath with nodelist

查看:92
本文介绍了带有节点列表的Apache Camel XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Apache Camel XPath结果中的所有值连接起来,并将其添加到消息上下文中.标头应如下所示:"|value1|value2|valueN|"

I would like to concatenate all values from the Apache Camel XPath result and add it to the message context. The header should look like: "|value1|value2|valueN|"

我的路线如下:

from("direct:test")
.setHeader("key").xpath("//Identifier", List.class)
.to("mock:result")

做到这一点的最佳方法是什么?有没有办法实现自己的结果类型?

What is the best way for doing that? Is there a way to implement an own result type?

推荐答案

正如Willem所说,您必须编写自己的处理器.

As Willem said, you have to write your own processor.

对于这种小事情,我最喜欢的方法是在包含路由定义的类中声明一个函数,该函数返回如下所示的匿名处理器:

For such a little thing, my favourite way is to declare a function in the class containing the route definition returning an anonymous Processor like this:

private Processor setHeaderWithIdentifiers() {
    return exchange -> {
        List<String> identifiers = new ArrayList<>();
        NodeList nodes = XPathBuilder.xpath("//Identifier").evaluate(exchange,  NodeList.class);

        for (int i = 0; i < nodes.getLength(); i++) {
            identifiers.add(nodes.item(i).getNodeValue());
        }

        // StringUtils from Apache Commons 3 
        String idAsString = StringUtils.join(identifiers, "|");
        exchange.getIn().setHeader("key", idAsString);
    };
}

这样,您不需要查找任何复杂的XPath函数,并且只要Processor代码简短,就可以清楚地理解代码.

With that, you do not need to find any complex XPath functions and the code remains clear to understand as long the Processor code remains short.

这篇关于带有节点列表的Apache Camel XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆