이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 22. JXPath


Overview

The JXPath language enables you to invoke Java beans using the Apache Commons JXPath language. The JXPath language has a similar syntax to XPath, but instead of selecting element or attribute nodes from an XML document, it invokes methods on an object graph of Java beans. If one of the bean attributes returns an XML document (a DOM/JDOM instance), however, the remaining portion of the path is interpreted as an XPath expression and is used to extract an XML node from the document. In other words, the JXPath language provides a hybrid of object graph navigation and XML node selection.

Adding JXPath package

To use JXPath in your routes you need to add a dependency on camel-jxpath to your project as shown in Example 22.1, “Adding the camel-jxpath dependency”.

Example 22.1. Adding the camel-jxpath dependency

<!-- Maven POM File -->
<properties>
  <camel-version>2.21.0.fuse-740039-redhat-00001</camel-version>
  ...
</properties>

<dependencies>
  ...
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jxpath</artifactId>
    <version>${camel-version}</version>
  </dependency>
  ...
</dependencies>

Variables

Table 22.1, “JXPath variables” lists the variables that are accessible when using JXPath.

Table 22.1. JXPath variables
VariableTypeValue

this

org.apache.camel.Exchange

The current Exchange

in

org.apache.camel.Message

The IN message

out

org.apache.camel.Message

The OUT message

Options

Table 22.2, “JXPath option” describes the option for JXPath.

Table 22.2. JXPath option
OptionTypeDescription

lenient

boolean

Camel 2.11/2.10.5: Allows to turn lenient on the JXPathContext. When turned on this option allows the JXPath expression to evaluate against expressions and message bodies which might be invalid or missing data. See more details at the JXPath Documentation. This option is false, by default.

Examples

The following example route uses JXPath:

<camelContext>
  <route>
    <from uri="activemq:MyQueue"/>
    <filter>
      <jxpath>in/body/name = 'James'</xpath>
      <to uri="mqseries:SomeOtherQueue"/>
    </filter>
  </route>
</camelContext>

The following simple example uses a JXPath expression as a predicate in a Message Filter:

{snippet:id=example|lang=java|url=camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java}

JXPath injection

You can use Bean Integration to invoke a method on a bean and use various languages, such as JXPath, to extract a value from the message and bind it to a method parameter.

For example:

public class Foo {
     @MessageDriven(uri = "activemq:my.queue")
     public void doSomething(@JXPath("in/body/foo") String correlationID, @Body String body)
     { // process the inbound message here }
   }

Loading the script from an external resource

Available as of Camel 2.11

You can externalize the script and have Camel load it from a resource such as "classpath:", "file:", or "http:". Use the following syntax:

"resource:scheme:location"

For example, to reference a file on the classpath:

.setHeader("myHeader").jxpath("resource:classpath:myjxpath.txt")
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.