Convert JAXB Object to XML in Mule ESB

In this tutorial I am going to show you how we can convert JAXB object into XML data in Mule ESB. We will use File Connector to take an XML file as input then we will map it to an appropriate JAXB object, then finally we will convert the JAXB object back into XML data.

You can see also Convert XML to JAXB Object in Mule ESB

The JAXB transformers allow objects to be serialized to XML and back again using the JAXB binding framework.

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program’s class structure.

Mule support binding frameworks such as JAXB and Jackson. These frameworks use annotations to describe how data is mapped to a Java object model. Reference


Mule Studio 3.x(Anypoint Studio) (Download from
Maven 3.2.1 (Download from
JDK 1.7 (Download from

Configure JDK, Maven and Mule Studio

Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.

Create Mule project in Mule Studio

Now we will see how to create a new project in Mule Studio(Anypoint Studio).

Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule-xml-java, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.

So when the project mule-xml-java is created in the Anypoint Studio, the project structure looks like below

mule xml to object

Convert XML file to JAXB object and JAXB object into XML data

mule jaxb to xml

Step 1. Open the mule-xml-java.xml file and click on Configuration XML view in the Editor
Step 2. Modify the mule-xml-java.xml file as shown below

In the above configuration we have first created the JAXB_Context using either spring:bean or mulexml:jaxb-context that is used later in mulexml:jaxb-xml-to-object-transformer.

Then we have <file:inbound-endpoint/> for taking an XML file as an input. Then we transform the file content or payload to string value using <file:file-to-string-transformer/>. We log the file content using <logger/>.

Next we transform the XML file data into JAXB Java object – we specify here the JAXB_Context to use for transforming and returnClass to what type of Java object it has to convert.

Then again we use <logger/> for logging message into the Console.

Next we have <mulexml:jaxb-object-to-xml-transformer/> for transforming the JAXB object into XML.

Finally we use <logger/> for logging the XML data into the Console.

Step 3. Create catalog.xsd file in src/main/resources/xsd directory with below content

Step 4. Add XJC plugin to the pom.xml file for generating Java object from XSD

Step 5. Now build the project, you will and have been generated in com.roytuts.jaxb.model package

Step 6. Create sample XML file catalog.xml with below data

Running the application

Now do a right-click on the mule-xml-java project and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs

Once the application is up and running put the catalog.xml file under D:AnypointWorkspace.

Console output

Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.