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

查看:26
本文介绍了带有节点列表的 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 函数,只要处理器代码保持简短,代码就会清晰易懂.

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天全站免登陆