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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

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

  • HTAP Using a Star Query on MongoDB Atlas Search Index
  • The Synergy of Security and Development: Integrating Threat Models With DevOps
  • Zero-Latency Architecture: Database Triggers + Serverless Functions for Modern Reactive Architectures
  • Turbocharge Load Testing: Yandex.Tank + ghz Combo for Lightning-Fast Code Checks
  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.3K 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

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
  • [email protected]

Let's be friends: