375.10. 在没有交换的情况下使用 XPathBuilder

可作为 Camel 2.3 提供

现在,您可以使用 org.apache.camel.builder.XPathBuilder,无需 Exchange。如果您想将其用作执行自定义 xpath 评估的帮助程序,这将很方便。

它要求您传递 CamelContext,因为 XPathBuilder 中的多个移动部分需要访问 Camel Type Converter,因此为什么需要 CamelContext。

例如,您可以做到这一点:

boolean matches = XPathBuilder.xpath("/foo/bar/@xyz").matches(context, "<foo><bar xyz='cheese'/></foo>"));

这将与给定的 predicate 匹配。

您还可以评估示例,如以下三个示例所示:

String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>", String.class);
Integer number = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>123</bar></foo>", Integer.class);
Boolean bool = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>true</bar></foo>", Boolean.class);

使用 String 结果进行评估是常见的要求,因此您可以做一些简单:

String name = XPathBuilder.xpath("foo/bar").evaluate(context, "<foo><bar>cheese</bar></foo>");