Configure XML Reader Properties for DataWeave in Mule

Mule 4 is new and there are many problems, it takes a lot of time for us to get familiar, so we know it at all. One of the problems that I’ve had recently with Mule 4 is how to change the default value of XML Reader Properties “nullValueOn” for DataWeave from “blank” to “none”.

If you know the “nullValueOn” attribute, this attribute is used when we transform data input as XML to any output using DataWeave. The content of our input XML data can have tags no appear, empty or blank. With Mule 3, the default value of “nullValueOn” is “none”, meaning that only when input XML data does not have the tag we want, DataWeave 1 will treat the tag as null. But in Mule 4, because the default value of this attribute is “blank”, meaning that the tags have no value, only the tag name, such as <id />, DataWeave 2 will also treat the tag with value as null. This may seem unreasonable because in this case, this tag still exists in the input data. But in terms of meaning, I also feel reasonable, because if the value is blank, then what is the purpose to output that tag? …

For you to better understand, I have an application written in Mule 3 as follows:

Configure XML Reader Properties for DataWeave in Mule

This same application is written in Mule 4 as follows:

Configure XML Reader Properties for DataWeave in Mule

Both versions of this application have an HTTP Listener endpoint, a Set Payload endpoint with the following content:

and a Transform Message endpoint.

The result when we request to http://localhost:8081/test of Mule 3 will be as follows:

Configure XML Reader Properties for DataWeave in Mule

And Mule 4 will be:

Configure XML Reader Properties for DataWeave in Mule

Can you see the difference? Mule 4 is considering the <id> tag in the input data as null, then should output an XML tag with nil = true.

We can change the default value of the “nullValueOn” attribute as well as other XML Reader Properties in Mule 3 and Mule 4. But the configuration of this attribute as well as other properties will be different for Mule 3 and Mule 4. Details:

For Mule 3, in the content of the <dw:transform-message> tag, you can add the <dw:input-payload> tag if your application does not already exist this tag, then in this <dw:input-payload> tag, add the <dw:reader-property> tag with name as the XML Reader Properties name which you want to change the default value. For example, I can configure the “nullValueOn” attribute for my application on Mule 3 as follows:

Requesting the application again, you will see the result as above Mule 4.

For Mule 4, the configuration is completely different.

Module 4 relies on the MIME Type of the input data object and we will configure the XML Reader Properties use this MIME Type. This means, if I want to change the default value of an input data, I need to change the MIME Type of that input data. For example, Mule 4 example above, my input data is Payload, its current MIME Type is application/xml, what we need to do is change this MIME Type to add the XML Reader Properties to it.

You can click on Set Payload endpoint and open the Set Payload tab:

Configure XML Reader Properties for DataWeave in Mule

Then, click on the MIME Type tab in this window and then add Parameter as follows:

Configure XML Reader Properties for DataWeave in Mule

XML content of Set Payload endpoint now will be:

You can see that the MIME Type of Set Payload endpoint has been configured to add XML Reader Properties “nullValueOn” with “application/xml; nullvalueon=none”.

Run the example again to see the results! …

One thing to note is that the configuration of the XML Reader Properties can also apply to other MIME Type such as application/json!

Add Comment