Over a million developers have joined DZone.

Mule 3.8 DataWeave Read for Lists MIME Type

Learn how a read sentence in DataWeave can save you time and effort in Mule 3.8.

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

Recently, I was faced with a problem while trying to apply a reduce operation on a list of XML strings; I was running Mule 3.7, and after more attempts than I care to remember, I had to adopt an overkilling workaround. Meanwhile, with Mule 3.8, there's a very simplye way to solve the problem.

Let's jump into the case: A Mule flows reads, once a day, from a repository where objects created and saved the day before are placed. Suppose our domain object is an order containing one or more books. The order is stored as an XML string:


After the read operation, the payload is a list of XML strings... but they're still strings!

Suppose we need to sum the number of books of each order to have the total number of books sold. The idea is to use DataWeave and apply map/reduce operators to get the job done. Of course, we need to somehow parse the XML structure of the order. The true story is that in order to parse the XML, you need to set the MIME type of the payload. But our payload is a list, and we need to set the MIME type for each element of the list, not to the list itself.

The only way is to use foreach, set the MIME type, process each order individually, and collect the total number of books in a variable:

<flow name="spikeweavexmlFlow_List_new">
    <set-session-variable variableName="numberOfBooks" value="#[0]" doc:name="NumberOfBooks = 0"/>
        <foreach collection="#[payload]" doc:name="For Each">
        <set-payload value="#[payload]" mimeType="application/xml" doc:name="Set XML mimetype"/>
            <dw:transform-message doc:name="count books">
                <![CDATA[%dw 1.0
                %output application/java
                    numberOfBooks: sizeOf payload.order.*book
         <set-session-variable variableName="numberOfBooks" value="#[numberOfBooks + payload.numberOfBooks]" doc:name="Increase NumberOfBooks"/>

A very redundant workaround.

With Mule 3.8, there is a very elegant solution: The read sentence in DataWeave that set the MIME type of the element:

<flow name="dzone-dataweave-readFlow">
    <dw:transform-message doc:name="Transform Message">
            <![CDATA[%dw 1.0
            %output application/java
            numberOfBooks: payload map ({
                numOfBooksPerXml: sizeOf read($,"application/xml").order.*book
            }).numOfBooksPerXml reduce $$ + $]]>

Basically, in the map operator, the value variable $ is now an XML element we can parse and have access to.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

mule 3.7,mule esb,muleesb

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}