Using xpath3() in Mule 3

When working with Mule ESB applications related to SOAP Web Service, we will often work, manipulating data, payloads in XML format. Using Dataweave can help us easily manipulate XML data, but there is another way you can use that is to use xpath3() in Mule. How is it in details? Let’s learn about the xpath3() of Mule in this tutorial!

First, I will create a Mule Maven project as an example:

Using xpath3() trong Mule 3

To make an example, I will create a Mule application with the following content:

Using xpath3() trong Mule 3

In particular, the HTTP Listener Connector can receive a POST request from the user with data in the body of the request in XML format. Then, we will use xpath3() in Mule to extract the value which we need from that data and print that value using the Huong Dan Java Logger.

HTTP Listener Connector that will have the following configuration:

Using xpath3() trong Mule 3

Global Configuration:

Using xpath3() trong Mule 3

Transformer Byte Array to String is used to convert Payload from the body data of the request in the form of a byte array to String.

Huong Dan Java Logger has the following configuration:

Using xpath3() trong Mule 3

Global Configuration:

Using xpath3() trong Mule 3

For example, I will request this application with data with the following content:

This is a SOAP Message!

Now we will talk about xpath3() in Mule.

xpath3() in Mule which is a function defined as follows:

Where:

– xPathExpression is a required parameter, used to define the expression to the element that we need to get the data.

– inputData is the input data that contains the data we need to retrieve, by default if you don’t declare this parameter, xpath3() will retrieve the input data from Payload,

– returnType is the data type that will return after the execute expression of xpath3() to get the result. By default, returnType will return java.lang.String if we don’t declare this value. Other values of returnType include: NUMBER (java.lang.Double type), BOOLEAN (java.lang.Boolean type), NODE (DOM XML Node type) and NODESET (DOM NodeList type).

To define xPathExpression with xpath3() you should define the following format:

with data as follows: “<Tag1> <Tag2> <Tag3> </Tag3> </Tag2> </Tag1>”. In this case, we need to get the contents of <Tag3>.

For example, with my above data, to get the content of the <name> tag, I will define xPathExpression as follows:

The results when I configured the Huong Dan Java Logger with the value:

will be as follows:

Using xpath3() trong Mule 3

In case the request message has the following content:

then extract the value of the <name> tag with attribute lastname=”Phung”, you need to define xPathExpression as follows:

or:

The result when we run the above example is as follows:

Using xpath3() trong Mule 3

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment