What is JAXB?

It has become common for enterprise applications to exchange, represent any data using XML. It is possible to represent binary and character based data in XML files.

Read here Advantages of storing data in XML format.

And it would be often required to convert an object to an XML and vice versa.

Process of converting an object to XML is called Marshalling. And the process of converting XML to an object is called Un Marshalling.

J2EE has provided JAXB for the same. JAXB stands for Java Architecture for XML Binding.

JAXB is provided in package javax.xml

Below is an example for Marshalling, i..e converting an object to its XML representation.
This example has POJO classes Address8 and Employee8.
Employee8 and Address8 has composition relationship.
Annotations are used in Employee8 and Address8 classes, to specify XML Root Element, XML Elements and XML attributes.
MainAppl8 class creates marshaller, and marshalls Employee8 object to my_emp.xml XML file.

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

     public class Address8 {
    private int id;
    private String city;
    private int postCode;
    public Address8(){}

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getCity() {
        return city;

    public void setCity(String city) {
        this.city = city;

    public int getPostCode() {
        return postCode;

    public void setPostCode(int postCode) {
        this.postCode = postCode;
import javax.xml.bind.annotation.XmlAttribute;  
import javax.xml.bind.annotation.XmlElement;  
import javax.xml.bind.annotation.XmlRootElement;  
import javax.xml.bind.annotation.XmlAccessorType;
public class Employee8 {  
private int id;  
private String name;  
private float salary;  
private int age;
private Address8 address;

public Employee8(){}

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 float getSalary() {  
    return salary;  
public void setSalary(float salary) {  
    this.salary = salary;  

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public Address8 getAddress() {
        return address;

    public void setAddress(Address8 address) {
        this.address = address;
import java.io.FileOutputStream;  
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.Marshaller;  
public class MainAppl8 {  
public static void main(String[] args) throws Exception{  
    JAXBContext contextObj = JAXBContext.newInstance(Employee8.class);  
    Marshaller marshallerObj = contextObj.createMarshaller();  
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
    Address8 adr=new Address8();
    Employee8 emp=new Employee8();
    emp.setName("Kiran Kumar");
    marshallerObj.marshal(emp, new FileOutputStream("my_emp.xml"));  

JAXB marshall example(Netbeans)

Below is an example for UnMarshalling, i..e converting XML to an object. In this example XML String is converted into Employee object, using JAXB.

public class Employee {
 private String firstName;
 private String lastName;
 private String fatherName;

 public String getFirstName() {
  return firstName;

 public void setFirstName(String firstName) {
  this.firstName = firstName;

 public String getLastName() {
  return lastName;

 public void setLastName(String lastName) {
  this.lastName = lastName;

  public String getFatherName() {
  return fatherName;

 public void setFatherName(String fatherName) {
  this.fatherName = fatherName;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class CreateEmployeeFromXml {
 public static void main(String[] args) {
  try {
   String xml = "<Employee><firstName>Kiran</firstName><lastName>Kumar</lastName><fatherName>Kishore</fatherName></Employee>";
   JAXBContext jaxbc = JAXBContext.newInstance(Employee.class);
   Unmarshaller unmarshaller = jaxbc.createUnmarshaller();
   StreamSource ss = new StreamSource(new StringReader(xml));
   JAXBElement jbe = unmarshaller.unmarshal(ss,
   Employee employee = (Employee)jbe.getValue();
   System.out.println("Employee's First Name is" +  employee.getFirstName());
   System.out.println("Employee's Last Name is" +  employee.getLastName());
   System.out.println("Employee's father Name is" +  employee.getFatherName());
  } catch (JAXBException e) {

JAXB unmarshall example(Netbeans)

You may also like to read:
What is DTD in XML?
How many Servlet filters can be used?

What is the difference between DTD and XSD

Before getting into the difference between DTD and XSD, lets take a look at XML, eXtensible Markup Language.

XML is basically used to store, exchange or transfer between Software Programs or Applications.
Almost any complex data can be represented using XML.
Most of the popular software languages and third party software provides XML parsers.
Application Settings or Configurations, are generally stored in a XML file.

Difference between XML and HTML

Though both are Markup Languages. Purpose of both XML and HTML is entirely different.
HTML(Hyper Text markup Language) is used to display web pages on Web Browser. Web Browsers like Chrome, Firefox, Internet Explorer,etc… parses and renders the HTML file being server, from Web Server.
In HTML tags are pre defined, and we cannot create new HTML Tags.
Another Advantage of XML is data represented or transferred is language and platform independent.

But purpose of XML is to store, exchange or transfer data.In XML we can create our own tags or elements, and we can specify name of the tag, data type, etc…

How DTD and XSD differ

1. Both DTD and XSD are used to describe an XML document.
2. DTD stands for Document Type Definition. XSD stands for XML Schema Docment.
3. DTD is older approach, where as XSD is latest one.
4. XSD has almost replaced DTD, though old ones continue to exist in DTD.
5. DTD provide only two data types CDATA and PCDATA, where as XSD provides more data types such as int, decimal, etc…
6. XSD has more control over restricting only certain values to an element or Attribute like only positive or negative values are allowed etc…, where as DTD do not restrict
7. XSD supports namespaces to avoid name collision between elements having same name.
8. XSD uses XML syntax, but DTD does not.

You may also like to read:
how to use Marquee tag in HTML
input tags in HTML