Learn JAXB
What is JAXB?
JAXB stands for Java Architecture for XML Binding. To work with JAXB you do not need to know about XML parses. You can get either XML document from Java object or vice versa.There are two mechanisms in JAXB.
- Marshalling
Marshalling is converting Java object to XML.
- Unmarshalling
Unmarshalling is converting XML to Java Object.Convert Java Object to XML (Marshalling)
Conversion of Java to XML is not a big task with JAXB. We will go in to this step by step.
First of all, we need to create a Java bean. That bean class is the class which we are going to convert.
Today I am going to write a simple Java bean for Employee. I am going to convert it in to an XML document.
Let's start work.
import javax.xml.bind.annotation.*; import java.util.List; /** * Created by dinithi on 7/18/14. */ public class Employee { private int id; private String name; private int age; private double salary; private List<Dependency> dependencies; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public List<Dependency> getDependencies() { return dependencies; } public void setDependencies(List<Dependency> dependencies) { this.dependencies = dependencies; } }
For the above bean class, I need the id of the employee to be a attribute and name,age and salary to be elements. So how can the JAXB understand whether this is an attribute or element, or else what is the root element?
For the above purpose, JAXB uses annotations. There are several annotations used by JAXB. We will discuss them one by one.
JAXB Annotations
@XmlRootElement Defines the root of the XML document which needs to be created. The class name will be the name of the root. |
@XmlElement Defines the property must be an element of the XML document |
@XmlAttribute Defines the property must be an attribute of the XML document |
@XmlTransient Defines the property should not be included in to the XML document |
@XmlType Defines the order of the elements in the XML document |
@XmlSeeAlso Includes another Java bean to the XML document |
@XmlElementWrapper(name='<name'>) Wraps existing set of elements with a tag specified by the name |
@XmlID Specifies a unique identity element or attribute |
Let's modify our example bean class with annotations.
import javax.xml.bind.annotation.*; import java.util.List; /** * Created by dinithi on 7/18/14. */ @XmlRootElement @XmlType(propOrder = { "id", "name", "age","salary","dependencies"}) public class Employee { private int id; private String name; private int age; private double salary; private List<Dependency> dependencies; public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getAge() { return age; } @XmlTransient
public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } @XmlElement public void setSalary(double salary) { this.salary = salary; } public List<Dependency> getDependencies() { return dependencies; } @XmlElementWrapper(name="dependencies") @XmlElement(name = "dependency") public void setDependencies(List<Dependency> dependencies) { this.dependencies = dependencies; } }
I think you can understand what happened. We have included some annotations to the Java bean. Look at the places where I add them.If you have declared your properties as private, you need to annotate them with the help of getters or setters. Or else, if you have public modifier you can annotate just in front of the property as follows.
@XmlAttribute public int id;
I think you can assume the output of this. What will be the output of the wrapper element? Can you guess?
It will create a tag with name dependencies and inside it there will be a list of dependency tags. So you may be thinking how the XML document will create a list of dependencies without knowing the values it need.
For that we need to create a bean class for dependency.
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; /** * Created by dinithi on 7/18/14. */ public class Dependency { private int id; private String name; private int age; Dependency(){} Dependency(int id, String name, int age){ this.id=id; this.name=name; this.age=age; } public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getAge() { return age; } @XmlElement public void setAge(int age) { this.age = age; } }
This Dependency class will provide necessary details for the dependencies list in the Employee class.
Up to now, we have completed our bean classes which we need to convert to XML document.
Let's write a Java class to convert this bean classes to XML.
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.File; import java.util.ArrayList; /** * Created by dinithi on 7/18/14. */ public class JAXBJavaToXml { public static void main(String[] args) { Employee employee1=new Employee(); employee1.setName("Mathan"); employee1.setAge(45); employee1.setId(1); employee1.setSalary(75000.00); ArrayList<Dependency> dependencyArrayList=new ArrayList<Dependency>(); Dependency dependency1=new Dependency(1,"Mirasha",7); dependencyArrayList.add(dependency1); Dependency dependency2=new Dependency(2,"Albian",3); dependencyArrayList.add(dependency2); employee1.setDependencies(dependencyArrayList); try { JAXBContext jaxbContext=JAXBContext.newInstance(Employee.class); Marshaller marshaller=jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); File xmlFile=new File("outputXML.xml"); marshaller.marshal(employee1,xmlFile); marshaller.marshal(employee1,System.out); } catch (JAXBException e) { e.printStackTrace(); } } }
When you compile and run this class it will create an XML document with the relevant details.
This is the output generated form the above bean classes.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee id="1"> <name>Mathan</name> <salary>75000.0</salary> <dependencies> <dependency id="1"> <age>7</age> <name>Mirasha</name> </dependency> <dependency id="2"> <age>3</age> <name>Albian</name> </dependency> </dependencies> </employee>
Convert XML to Java Object (Unmarshalling)
And also we can convert an XML into a Java object.Let's talk about unmarshalling with my next session. See you soon...