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:
To make an example, I will create a Mule application with the following content:
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:
Global Configuration:
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:
Global Configuration:
For example, I will request this application with data with the following content:
1 2 3 4 5 6 7 8 |
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <hdj:hello xmlns:hdj="http://huongdanjava.com/"> <name lastname="Nguyen">Khanh</name> <age>31</age> </hdj:hello> </soap:Body> </soap:Envelope> |
This is a SOAP Message!
Now we will talk about xpath3() in Mule.
xpath3() in Mule which is a function defined as follows:
1 |
xpath3(xPathExpression, inputData, returnType) |
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:
1 |
/*:Tag1/*:Tag2/*:Tag3 |
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:
1 |
/*:Envelope/*:Body/*:hello/*:name |
The results when I configured the Huong Dan Java Logger with the value:
1 |
Extracted value: #[xpath3("/*:Envelope/*:Body/*:hello/*:name")] |
will be as follows:
In case the request message has the following content:
1 2 3 4 5 6 7 8 |
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <hdj:hello xmlns:hdj="http://huongdanjava.com/"> <name lastname="Nguyen">Khanh</name> <name lastname="Do">Phung</name> </hdj:hello> </soap:Body> </soap:Envelope> |
then extract the value of the <name> tag with attribute lastname=”Phung”, you need to define xPathExpression as follows:
1 |
/*:Envelope/*:Body/*:hello/*:name[2] |
or:
1 |
/*:Envelope/*:Body/*:hello/*:name[@*:lastname=\'Do\'] |
The result when we run the above example is as follows: