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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Payload Transformation: XML to Object

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.

Christopher Co user avatar by
Christopher Co
·
Apr. 30, 17 · Tutorial
Like (3)
Save
Tweet
Share
12.76K Views

Join the DZone community and get the full member experience.

Join For Free

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.

XML Payload (computing) Object (computer science) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Securing Cloud-Native Applications: Tips and Tricks for Secure Modernization
  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • Getting a Private SSL Certificate Free of Cost
  • Create a CLI Chatbot With the ChatGPT API and Node.js

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: