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.
Join the DZone community and get the full member experience.
Join For FreeJava 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", "john@springframework.guru");
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.
Using JAXB to Unmarshall XML into Java Objects
Unmarshalling XML with JAXB to a Java object involves creating an Unmarshaller on the JAXBContext and calling the unmarshal() method. This method accepts the XML file to unmarshal.
The JUnit test class to unmarshal the generated product.xml back to the Product and User objects is this:
XmlToProductTest.java:
package guru.springframework.blog.unmarshal;
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 javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
public class XmlToProductTest {
private Product product;
@Test
public void testXmlToObject() throws JAXBException, FileNotFoundException {
File file = new File("product.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
product = (Product) unmarshaller.unmarshal(file);
System.out.println(product);
}
}
In this XmlToProductTest class, a JAXBContext initialized with Product is used. The createUnmarsheller() method returns an Unmarshaller. A JAXB Unmarshaller governs the process of unmarhshalling XML data into a Java object tree. Finally, the unmarshal() method unmarshals the File object for product.xml into the Product POJO.
The output of running the test is this:
Handling Collections
Often you will need to marshal Java collection objects, such as List, Set, and Map to XML, and also unmarshal XML back to collection objects.
Consider our current application, where we now need to work with a list of products. To model the new requirement, let us create a Products class.
The code of the Products class is:
Products.java:
package guru.springframework.blog.domain;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement(name = "products")
public class Products {
List<Product> products;
public List<Product> getProducts() {
return products;
}
@XmlElement(name = "product")
public void setProducts(List<Product> products) {
this.products = products;
}
public void add(Product product) {
if (this.products == null) {
this.products = new ArrayList<Product>();
}
this.products.add(product);
}
}
In this Products class, the @XmlRootElement annotation specifies the root element of the XML as products. This class has a single List property with getter and setter methods. The add() method of this class accepts a Product object and adds it to the List.
The test class to convert a list of products to XML is:
ProductToXmlListTest.java:
package guru.springframework.blog.marshal;
import guru.springframework.blog.domain.Product;
import guru.springframework.blog.domain.Products;
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 javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
public class ProductToXmlListTest {
private Product product;
private Product product1;
@Before
public void setUp(){
long l = 10;
Long longId = new Long(l);
User user = new User(longId,"John","john@springframework.guru");
product = new Product("PO1", "Spring Guru Mug","https://springframework.guru/wp-content/uploads/2017/12/spring_framework_guru_mug-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg", new BigDecimal(18.95),user);
product1 = new Product("PO2", "Spring Guru Shirt","https://springframework.guru/wp-content/uploads/2017/12/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg", new BigDecimal(20.00),user);
}
@After
public void tearDown(){
product = null;
}
@Test
public void testObjectToXml() throws JAXBException, FileNotFoundException {
Products products = new Products();
products.add(product);
products.add(product1);
JAXBContext jaxbContext = JAXBContext.newInstance(Products.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(products, new File("products.xml"));
marshaller.marshal(products, System.out);
}
}
The output of running the test is:
Summary
In this post, I have covered the introductory concepts of JAXB. I have previously written a post on how to use an XML Schema and JAXB to generate Java classes for RESTful web services here.
JAXB also comes with a JAXB Binding compiler tool named schemagen. You can use schemagen to generate an XSD schema from Java classes.
The source code for this post is available on GitHub. You can download it here.
Published at DZone with permission of John Thompson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments