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
Partner Zones Build AI Agents That Are Ready for Production
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
Build AI Agents That Are Ready for Production

Related

  • Migrate Serialized Java Objects with XStream and XMT
  • What Is Ant, Really?
  • How to Convert XLS to XLSX in Java
  • Writing DTOs With Java8, Lombok, and Java14+

Trending

  • 12 Factor Framework for Building Secure and Compliant Cloud Applications
  • Your Codename One App, Now A Native Mac App
  • You Already Have an AI Working Agreement. Write It Down.
  • Building Reliable Async Processing Pipelines Using Temporal
  1. DZone
  2. Coding
  3. Languages
  4. Using JAXB for XML With Java

Using JAXB for XML With Java

Let's dive into JAXB and how to use it to help for marshalling and unmarshalling objects to and from XML in Java.

By 
John Thompson user avatar
John Thompson
·
Feb. 11, 18 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
259.4K Views

Join the DZone community and get the full member experience.

Join For Free

Java Architecture for XML Binding (JAXB) is a library that helps bind XML schemas and Java representations. JAXB provides you with a mechanism to marshal Java objects into XML and the other way around – unmarshal XML into Java objects.

XML is an industry standard for defining the contents of your message. XML, along with Java, are complementary technologies to exchange data across the Internet. When you work with XML, you need a way to take an XML file, then convert it into some sort of data structure, which your program can manipulate. You also need to serialize the state of your Java objects into XML. JAXB is one library that performs such marshalling and unmarshalling operations.

In this post, I will discuss on how to marshal Java objects into XML and the other way around using JAXB.

Using JAXB to Marshal Java Objects to XML

Let us consider a scenario where we need to model a product for an e-commerce store. Apart from fields, such as product Id, description, image URL, and price, a Product object is also composed of a User object that represents the user who adds the product to the store.

Our intent is to marshal the Product object, along with its composed User object into XML by using JAXB.

We can start by creating a User POJO to model the user.

User.java:

package guru.springframework.blog.domain;

public class User {
    private Long id;
    private String name;
    private String email;

    public User() {
    }

    public User(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public User setId(Long id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public User setName(String name) {
        this.name = name;
        return this;
    }

    public String getEmail() {
        return email;
    }

    public User setEmail(String email) {
        this.email = email;
        return this;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("User{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", email='").append(email).append('\'');
        sb.append('}');
        return sb.toString();
    }
}


User is a simple POJO with the id, name, and email fields, along with their corresponding getter and setter methods.

Next, we will create the Product POJO. While creating this POJO, we will use annotations introduced in JAXB 2.0 to control how our Product object is marshalled to XML.

Note: As of JDK 1.6, JAXB is bundled with the JDK. Therefore, you don’t need to add any dependency for it.

The code of the Product POJO is below.

Product.java

package guru.springframework.blog.domain;

import javax.xml.bind.annotation.*;
import java.math.BigDecimal;

@XmlRootElement(name = "product")
//@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
    @XmlAttribute(name = "id")
    private String productId;
    @XmlElement(name = "description")
    private String description;
    @XmlElement(name = "imageUrl")
    private String imageUrl;
    @XmlElement(name = "price")
    private BigDecimal price;
    @XmlElement(name = "createdBy")
    private User createdBy;
    public Product(){}
    public Product(String productId, String description, String imageUrl,
                   BigDecimal price, User createdBy) {
        this.productId = productId;
        this.description = description;
        this.imageUrl = imageUrl;
        this.price = price;
        this.createdBy = createdBy;
    }

    @Override
    public String toString() {
        return "Product{" +
                "\n productId='" + productId + '\'' +
                ",\n description='" + description + '\'' +
                ",\n imageUrl='" + imageUrl + '\'' +
                ",\n price=" + price +
                ",\n createdBy=" + createdBy +"\n"+
                '}';
    }
}


In this Product class, we used a number of JAXB annotations. They are:

  • @XmlRootElement: This annotation is used at the top level class to indicate the root element in the XML document. The name attribute in the annotation is optional. If not specified, the class name is used as the root XML element in the document.
  • @XmlAttribute: This annotation is used to indicate the attribute of the root element.
  • @XmlElement: This annotation is used on the properties of the class that will be the sub-elements of the root element.

The Product POJO is now ready to be marshalled into XML. To do so, let us write a JUnit test class. If you are new to JUnit, I suggest going through my Unit Testing with JUnit series.

The test class is:

ProductToXmlTest.java:

package guru.springframework.blog.marshal;

import guru.springframework.blog.domain.Product;
import guru.springframework.blog.domain.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;

public class ProductToXmlTest {
    private Product product;

    @Before
    public void setUp() {
        long l = 10;
        Long longId = new Long(l);
        User user = new User(longId, "John", "[email protected]");
        product = new Product("PO1", "Spring Guru Mug", "https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg", new BigDecimal(18.95), user);
    }

    @After
    public void tearDown() {
        product = null;
    }

    @Test
    public void testObjectToXml() throws JAXBException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(product, new File("product.xml"));
        marshaller.marshal(product, System.out);
    }
}


In this code:

  • Line 19 – Line 24: Instantiates and initializes a User and a Product object in the setup() method marked as @Before.
  • Line 33: The JAXBContext.newInstance() method obtains a JAXBContext for the Product class that you want to marshal. JAXBContext provides the entry point for the JAXB API.
  • Line 34: Creates a Marshaller through a call to the createMarshaller() method of JAXBContext. In JAXB, the Marshaller class governs the process of marshalling Java objects into XML data through its various marshalling methods.
  • Line 35: Configures the Marshaller. The true value of the JAXB_FORMATTED_OUTPUT property instructs the Marshaller to generate the XML with proper indentation.
  • Line 36: Calls the marshal() method on the Marshaller with the initialized Product object and the file to write the XML.
  • Line 37: Marshals the object to the “standard” output stream.

On running the ProductToXmlTest class, a product.xml file gets generated.

The output of the test is: