Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JAXB Tutorial


JAXB tutorial provides concepts and API to convert object into XML and XML into object. Our JAXB tutorial is designed for beginners and professionals.

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.

jaxb1

Features of JAXB 2.0

JAXB 2.0 includes several features that were not present in JAXB 1.x. They are as follows:

1) Annotation support: JAXB 2.0 provides support to annotation so less coding is required to develop JAXB application. The javax.xml.bind.annotation package provides classes and interfaces for JAXB 2.0.

2) Support for all W3C XML Schema features: it supports all the W3C schema unlike JAXB 1.0.

3) Additional Validation Capabilities: it provides additional validation support by JAXP 1.3 validation API.

4) Small Runtime Library: it required small runtime library that JAXB 1.0.

5) Reduction of generated schema-derived classes: it reduces a lot of generated schema-derived classes.


Simple JAXB Marshalling Example: Converting Object into XML

Let’s see the steps to convert java object into XML document.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Marshaller objects
  • Create the content tree by using set methods
  • Call the marshal method
File: Employee.java
  1. import javax.xml.bind.annotation.XmlAttribute;
  2. import javax.xml.bind.annotation.XmlElement;
  3. import javax.xml.bind.annotation.XmlRootElement;
  4. @XmlRootElement
  5. public class Employee {
  6.     private int id;
  7.     private String name;
  8.     private float salary;
  9. public Employee() {}
  10. public Employee(int id, String name, float salary) {
  11.     super();
  12.     this.id = id;
  13.     this.name = name;
  14.     this.salary = salary;
  15. }
  16. @XmlAttribute
  17. public int getId() {
  18.     return id;
  19. }
  20. public void setId(int id) {
  21.     this.id = id;
  22. }
  23. @XmlElement
  24. public String getName() {
  25.     return name;
  26. }
  27. public void setName(String name) {
  28.     this.name = name;
  29. }
  30. @XmlElement
  31. public float getSalary() {
  32.     return salary;
  33. }
  34. public void setSalary(float salary) {
  35.     this.salary = salary;
  36. }
  37. }

@XmlRootElement specifies the root element for the xml document.

@XmlAttribute specifies the attribute for the root element.

@XmlElement specifies the sub element for the root element.

File: ObjectToXml.java
  1. import java.io.FileOutputStream;
  2. import javax.xml.bind.JAXBContext;
  3. import javax.xml.bind.Marshaller;
  4. public class ObjectToXml {
  5. public static void main(String[] args) throws Exception{
  6.     JAXBContext contextObj = JAXBContext.newInstance(Employee.class);
  7.     Marshaller marshallerObj = contextObj.createMarshaller();
  8.     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  9.     Employee emp1=new Employee(1,“Vimal Jaiswal”,50000);
  10.     marshallerObj.marshal(emp1, new FileOutputStream(“employee.xml”));
  11. }
  12. }

Output:

The generated xml file will look like this:

File: employee.xml
  1. <?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
  2. <employee id=“1”>
  3.     <name>Vimal Jaiswal</name>
  4.     <salary>50000.0</salary>
  5. </employee>

Simple JAXB UnMarshalling Example: Converting XML into Object

File: XMLToObject.java
  1. import java.io.File;
  2. import javax.xml.bind.JAXBContext;
  3. import javax.xml.bind.JAXBException;
  4. import javax.xml.bind.Unmarshaller;
  5. public class XMLToObject {
  6. public static void main(String[] args) {
  7.      try {
  8.             File file = new File(“employee.xml”);
  9.             JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
  10.             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  11.             Employee e=(Employee) jaxbUnmarshaller.unmarshal(file);
  12.             System.out.println(e.getId()+” “+e.getName()+” “+e.getSalary());
  13.           } catch (JAXBException e) {e.printStackTrace(); }
  14. }
  15. }

Output:

1 Vimal Jaiswal 50000.0

download this example (developed using eclipse IDE)

Comments

comments