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

Related

  • The Hidden Engineering Cost of XML in Enterprise Development Workflows
  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Loading XML into MongoDB

Trending

  • A Walk-Through of the DZone Article Editor
  • Deployment Lessons You Only Learn the Hard Way
  • Mocking Kafka for Local Spring Development
  • How You Clear Your HTML5 Canvas Matters
  1. DZone
  2. Coding
  3. Languages
  4. Mule and JAXB: turning an XSD file into an XML Fiesta!

Mule and JAXB: turning an XSD file into an XML Fiesta!

By 
Mariano Gonzalez user avatar
Mariano Gonzalez
·
Apr. 04, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
11.3K Views

Join the DZone community and get the full member experience.

Join For Free

Hello friends! How’s it going?

Has the following ever happened to you? You show up to work one morning and your boss tells you, “I need you to take this data and turn it into XML.” Well, this has happened to me, and in this blog post I’m going to show you how to do this quickly.

XSD?

In all fairness to my boss, he did give me an XSD file describing the structure of the XML I was asked to generate. But what is an XSD file anyway?

XSD stands for XML Schema Definition. It’s nothing but another XML file of a known and canonical format which is used to describe the structure of another XML file. For example, if I want to dump an employee’s data into an XML, the XSD’s job is to let everyone know that an employee must have a name, an address, a social security number, that he can hold several positions in the same organization and so forth… But most importantly, it describes the layout of all that data in the XML file.

Here’s a sample XSD example for your reference describing an employees XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Customer" type="CustomerType" />
  <xsd:complexType name="CustomerType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="address" type="xsd:string" />
      <xsd:element name="email" type="xsd:string" />
      <xsd:element maxOccurs="unbounded" name="InteractionResource" type="InteractionResourceType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="InteractionResourceType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="feed" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Generating objects

But, who cares? What’s the use of an XSD file? Well, for starters, your IDE probably uses XSD files to validate that the XML files you write are valid (this is true for example when working with Spring, Hibernate, and of course Mule ESB). But it could also be used for automatic mapping and code generation. What JAXB does is to read the XSD file to automatically generate a set of classes that mimic the structure of the XML and that allows for storing the same data in the same way. Once those classes exists, it’s easy for JAXB to marshall XML data into those objects and vice versa.

JAXB has a terminal command that takes an XSD file and turns it into a Java Bean. This command is called XJC and is present on the bin/ folder of any JDK installation since version 1.6. Here’s a sample of how to use it:

xjc example.xsd

The command above will create a java class with the proper JAXB annotations to perform XML marshalling and unmarshalling. It will also create a second class called ObjectFactory, which it will use internally when performing the transformations. You need to add these classes into your project. For simplicity let’s assume that you put them in the package com.mulesoft.example.

Then it’s just a matter of populating the bean and using Mule’s JAXB Transformers to generate the XML. Sample code looks as follows:

<!-- give jaxb a package to scan for annotations -->
<mulexml:jaxb-context name="myJaxB" packageNames="com.mulesoft.example" />
<sub-flow name="xmlMarshalling">
   
   <!-- sample transformer that would populate your bean -->
   <expression-transformer expression="new Employee(name:'John Doe', email:'[email protected]')" evaluator="groovy" />

   <!-- Generate the XML -->
   <mulexml:jaxb-object-to-xml-transformer name="myMarshaller" jaxbContext-ref="myJaxB"/>
</sub-flow>

That’s it. You just made your boss happy. Do you really want to impress him though? Let’s also see how you can do the reverse operation and transform an XML file into a Java Object.
Mule already provides an object-to-xml-transformer out of the box and it would work just fine in this case, but just for the sake of completeness, let’s see how you can do the same thing using JAX.

<sub-flow name="unmarshall">
    <mulexml:jaxb-xml-to-object-transformer name="XmlToObject" jaxbContext-ref="myJaxb" returnClass="org.mule.jaxb.Example"/>
</sub-flow>

And that’s it! You’re all set! Now show it to your boss and get him to buy you beer!

No related posts.




XML

Published at DZone with permission of Mariano Gonzalez. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Hidden Engineering Cost of XML in Enterprise Development Workflows
  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Loading XML into MongoDB

Partner Resources

×

Comments

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

  • 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