1) First we need to create jaxb context as shown below. A instance of this class is always required, as it is through this class we get marshaller and unmarshaller objects
JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);
An instance of JAXBContext is thread safe but it is heavy weight object. So it is recommended to reuse the same instance.
An instance of JAXBContext contains information necessary to map a class and class property to element and its subelements and vice versa. This mapping information is necessary to marshal and unmarshal.
2) We get the marshaller and unmarshaller object from jaxbContext.
Marshaller marshaller = jaxbContext.createMarshaller();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Marshaller object will convert java instances to xml format
Unmarshaller object will convert data in xml format to java instances
3) We need annotate the class whose instance has to be marshalled or unmarshalled as shown below
@XmlRootElement
public class Country {
…
}
If we dont an MarshalException is thrown
XmlRootElement annotation indicates that “Country” will be top element in the xml document.
We dont need to annotate the properties unless you want to customize the mapping information like changing the element name which will be created or referred during marshalling and unmarshalling. By default the property name is used as element name.
4) We marshall an object using the below statement
marshaller.marshal(country, fw);
Where “fw” is a file writer
5) We unmarshall the object using the below statement
country = (Country)unmarshaller.unmarshal(file);
Where “file”” represents file in the system.
Below is the complete code
Country class
package JAXB;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Country {
private ZipCode code;
private String name;
private int population;
public ZipCode getCode() {
return code;
}
public void setCode(ZipCode code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append("code:").append(this.code).append(",");
sb.append("name:").append(this.name).append(",");
sb.append("population:").append(this.population).append("}");
return sb.toString();
}
}
ZipCode class
package JAXB;
public class ZipCode {
private int part1;
private int part2;
public int getPart1() {
return part1;
}
public void setPart1(int part1) {
this.part1 = part1;
}
public int getPart2() {
return part2;
}
public void setPart2(int part2) {
this.part2 = part2;
}
}
Main class
package JAXB;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JAXBMarshallDemo {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);
Marshaller marshaller = jaxbContext.createMarshaller();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ZipCode zipCode = new ZipCode();
zipCode.setPart1(1234);
zipCode.setPart2(5678);
Country country = new Country();
country.setCode(zipCode);
country.setName("India");
country.setPopulation(10000000);
File file = new File("result.xml");
FileWriter fw = new FileWriter(file);
marshaller.marshal(country, fw);
country = null;
country = (Country)unmarshaller.unmarshal(file);
}
}
Output
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<country>
<code>
<part1>1234</part1>
<part2>5678</part2>
</code>
<name>India</name>
<population>10000000</population>
</country>