Mapping XML to Java Using Smooks Mediator
Want to transform XML messages into Java objects? See how you can configure WSO2's Smooks Mediator to lend a hand with that.
Join the DZone community and get the full member experience.
Join For FreeIn this post, we will show how we can use Smooks Mediator to transform an XML message into Java objects.
In this example, we will map an XML Message into a list of HashMaps.
Smooks Config File
For this example, we are going to parse a message like below:
<order>
<order-items>
<order-item>
<product>111</product>
<quantity>2</quantity>
<price>8.90</price>
</order-item>
<order-item>
<product>222</product>
<quantity>7</quantity>
<price>5.20</price>
</order-item>
</order-items>
</order>
Now let us see the Smooks Config file:
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd">
<jb:bean beanId="orderItems" class="java.util.ArrayList" createOnElement="order">
<jb:wiring beanIdRef="orderItem" />
</jb:bean>
<jb:bean beanId="orderItem" class="java.util.HashMap" createOnElement="order-item">
<jb:value property="productId" decoder="Long" data="order-item/product" />
<jb:value property="quantity" decoder="Integer" data="order-item/quantity" />
<jb:value property="price" decoder="Double" data="order-item/price" />
</jb:bean>
</smooks-resource-list>
This Smooks Config is mapping each order-item element into a HashMap instance. For each jb:value tag, it will add a HashMap entry using the property attribute as a key, and the data attribute will refer to the XML tag that holds the value.
This Smooks configuration is exporting the XML mapping as a JavaResult. With a JavaResult bean, we can get the beans created during the mapping using the value used in the beanId property.
ProxyService
Follow the code for the ProxyService that will use the Smooks Configuration.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="TestProxy" startOnLoad="true" statistics="disable" trace="disable" transports="http,https">
<target>
<inSequence>
<log level="custom">
<property name="STATUS" value="SmooksTest"/>
</log>
<payloadFactory media-type="xml">
<format>
<order>
<order-items>
<order-item>
<quantity>2</quantity>
<product>111</product>
<price>8.90</price>
</order-item>
<order-item>
<price>5.20</price>
<product>222</product>
<quantity>7</quantity>
</order-item>
</order-items>
</order>
</format>
<args/>
</payloadFactory>
<smooks config-key="Smooks-test">
<input type="xml"/>
<output property="javaResult" type="java"/>
</smooks>
<log level="custom">
<property name="STATUS" value="PROCESSED MSG**********"/>
</log>
<script language="js">print(mc.getProperty('javaResult').getBean("orderItems"));</script>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
<description/>
</proxy>
Basically:
- We are declaring a test payload using the payloadFactory mediator. This is the payload that the Smooks Mediator is going to process.
- We use Smooks Mediator. We refer to the Smooks config file using the config-key attribute. In this case, we are deploying the config file as a LocalEntry.
- The input is XML. We declare the output as Java, and we define the property name that the JavaResult generated in the transformation will be exported to.
- We use a script mediator to use the Java Object generated on the transformation.
If we invoke the proxy service in the TryIt tool, we will see something like this in the console logs:
[2017-11-19 17:26:36,931] [] INFO - LogMediator STATUS = PROCESSED MSG**********
[{quantity=2, productId=111, price=8.9}, {quantity=7, productId=222, price=5.2}]
In the log, we can see the toString output of the List of HashMaps.
We can also map the XML to a Java bean instead of HashMaps. We can use those Java objects inside a custom mediator and apply some custom processing.
I hope you enjoyed.
See you in the next post!
Published at DZone with permission of Francisco Ribeiro, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments