DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

Trending

  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Going All In On the JVM 25 Virtual Threads
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Mapping XML to Java Using Smooks Mediator

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.

By 
Francisco Ribeiro
·
Dec. 04, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In 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:

  1. We are declaring a test payload using the payloadFactory mediator. This is the payload that the Smooks Mediator is going to process.
  2. 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.
  3. 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.
  4. 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!

XML Mediator (software) Java (programming language)

Published at DZone with permission of Francisco Ribeiro. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook