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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • How to Practice TDD With Kotlin
  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 user avatar
Francisco Ribeiro
·
Dec. 04, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. 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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!