Thursday, July 20, 2017

Mule ESB - How to read xml payload content?

In this blog, I'm going to explain the way we can access the XML payload content and assign into a variable.

For an example, we will assume that mule flow received a xml message as below
<persons>
    <person>
        <name>Tharanga</name>
        <city>Colombo</city>
        <address>
            <street>1st Cross Street</street>
            <postalCode>10250</postalCode>
            <country>Sri Lanka</country>
        </address>
    </person>
</persons>

If we need to read the name of the person, have to write a mule expression as below
#[xpath://persons/person/name]

Since this is a xml payload, we need to use the xpath prefix to access the payload.

I used below sample flow to check this scenario.
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
 xmlns:spring="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8089" doc:name="HTTP Listener Configuration"/>
    <flow name="demo-readpayloadFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/payload" doc:name="HTTP"/>
        <set-variable variableName="messageid" value="#[xpath://persons/person/name]" doc:name="Variable"/>
        <logger message="#[flowVars.messageid]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[flowVars.messageid]" doc:name="Set Payload"/>
    </flow>
</mule>

When invoking the flow, we can see the value in the console according to the above sample.
INFO  2017-07-21 09:24:16,065 [[demo-readpayload].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Tharanga

Likewise, you can read the xml payload.

If the payload comes with the namespaces, you need to define the namespaces in the mulexml:namespace-manager tag as below
<mulexml:namespace-manager includeConfigNamespaces="true">
 <mulexml:namespace prefix="abc" uri="http://sample.lk/test" />
</mulexml:namespace-manager>
For an example, we will assume that payload comes as follows
<persons xmlns:abc="http://sample.lk/test">
    <abc:person>
        <abc:name>Tharanga</abc:name>
        <abc:city>Colombo</abc:city>
        <abc:address>
            <abc:street>1st Cross Street</abc:street>
            <abc:postalCode>10250</abc:postalCode>
            <abc:country>Sri Lanka</abc:country>
        </abc:address>
    </abc:person>
</persons>
And then need to change the mule expression as below
#[xpath://persons/abc:person/abc:name]

Likewise, you can read the xml payload when comes with the namespaces.

Enjoy..!!


2 comments:

  1. Thank you for sharing wonderful information with us to get some idea about that content.
    Mulesoft Online Training

    ReplyDelete
  2. After seeing your article, I want to say that the presentation is very good and also a well written article with very good information which is very useful for the readers .... thank you for sharing it and do share more posts like this.
    Mulesoft Online Training
    Mulesoft Training in Hyderabad

    ReplyDelete