Payload Transformation: XML to Object

DZone 's Guide to

Payload Transformation: XML to Object

Part 3 of this Payload Transformations series will show you how to transform an XML payload to a Java Object with Java classes and object transformers.

· Integration Zone ·
Free Resource

This is Part 3 of our Payload Transformation series; if you would like to check out the previous article about payload transformation, you can find it here

When constructing a Mule flow, we often encounter many different types of payloads, such as XML, JSON, and Object.

In this article, we are going to tackle handling XML payloads- more specifically, transforming an XML payload into a Java object.

We are going to use the following payload as an example:

Image title

Using XML to Object

Let's create our flow.

We create an HTTP inbound endpoint and an XML to Object transformer component.

Our flow should look like this:

Image title

Try running the program. If you notice, the flow will throw an error. The XML payload does not automatically get converted into a Java Object.

The Workaround

Create a Java Class

In order to successfully transform our XML payload, we must first define a Java class.

We will name our Java class "Student."

Our Java class should look like this:

Image title

The variable names indicated in our Java class should match the XML elements' name. Note, the names are case sensitive.

Configure The XML to Object Transformer

After defining our Java class, we will then configure our XML to Object Transformer Component.

Let's define our driver class (e.g. Xpp3Driver).

Image title

In the Advanced Tab, we will add an alias. The name of the alias should match the root element's name in our XML payload. In this case, we are going to use the name "Student."

The Class Name should contain the name of the package myschool.model.Student together with the name of our java class, Student.

Image title

Once we have configured our XML to Object transformer component, let's try running our program. We have successfully transformed the XML payload into Student object.

Image title

Taking It Further

What if you would like to transform a more complex XML?

Image title

Now, we have a new element called "subjects" with a list of subject elements.

Redefining the Java Class

In order for us to transform the XML payload successfully, we must first redefine the Java class "Student" that we created earlier.

Add the following code in Student.java:

 private List subjects;  

Remember, our variable name should match our XML payload's element name, subjects.

We also need to generate GET and SET methods for our new variable.

Image title

Since we don't have a Subject class, we need to create a Java class named "Subject."

Subject.java should look like this:

Image title

Reconfigure XML to Object Transformer

Once the Java classes are ready, we then reconfigure our transformer component.

In the Advanced tab, add another alias named subject under the Class Name myschool.model.Subject:

Image title

Let's try running our new program.

Image title

The XML payload has been successfully converted into our new Java object!

If you have any comments or suggestions that you would like to get tackled that were not mentioned in this article, please comment down below.

integration, java, mule, tutorial, xml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}