Skip to main content

JAXB

JAXB stands for java architecture for xml binding.

JAXB provide two main feature
1. Marshalling
2. Unmarshalling

Marshalling

converting java object into xml is knows as Marshalling.

Unmarshalling

Converting xml to java object is known as Unmarshalling.

JAXB annotation:


@XmlRootElement
This annotation define the root element of xml.

@XmlElement
This annotation is used to define xml element

@XmlType
This annotation is used define order of Xml element.

@XmlTransient
Field which you do not want to include in XML.

@XmlElementWrapper:
Generates a wrapper element around XML representation.

Marshalling Example:


Create java Project

Emp_info.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
package com.spy.javaforlearn;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name="employee")
 
public class Emp_info 
{
 
	String employee_name;
	int emp_id;
	double sal;
 
	@XmlElement(name="emp_name")
	public String getEmployee_name() 
	{
		return employee_name;
	}
 
	public void setEmployee_name(String employee_name) 
	{
		this.employee_name = employee_name;
	}
	@XmlElement(name="emp_id")
	public int getEmp_id() 
	{
		return emp_id;
	}
 
 
	public void setEmp_id(int emp_id) 
	{
		this.emp_id = emp_id;
	}
 
	@XmlElement(name="salary")
	public double getSal() 
	{
		return sal;
	}
	public void setSal(double sal) 
	{
		this.sal = sal;
	}
}

Company.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
package com.spy.javaforlearn;
 
import java.util.List;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Company 
{
    @XmlElement(name="employee")
	List <Emp_info> list_emp;
 
 
	public List<Emp_info> getList_emp() 
	{
		return list_emp;
	}
 
	@Override
	public String toString() {
		return "Company [list_emp=" + list_emp + "]";
	}
 
	public void setList_emp(List<Emp_info> list_emp) 
	{
		this.list_emp = list_emp;
	}
 
}

Marshalling.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
package com.spy.javaforlearn;
 
import java.io.File;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class Marshalling  
{
 
	public static void main(String[] args) 
	{
 
		Emp_info emp1=new Emp_info();
 
		emp1.setEmployee_name("bhavesh");
		emp1.setEmp_id(125);
		emp1.setSal(50221);
 
                Emp_info emp2=new Emp_info();
 
          	emp2.setEmployee_name("abhi");
		emp2.setEmp_id(124);
		emp2.setSal(50200);
 
 
		List<Emp_info> list=new ArrayList<Emp_info>();
 
		list.add(emp1);
		list.add(emp2);
 
 
		Company cmp=new Company();
 
		cmp.setList_emp(list);
 
 
		try
 
		{
 
		JAXBContext context=JAXBContext.newInstance(Company.class);
 
		Marshaller marshaller=context.createMarshaller();
 
		marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
 
		marshaller.marshal(cmp,System.out);
 
 
		marshaller.marshal(cmp, new File("C:/workspace/JavaJAXB/Target/company.xml"));
 
		System.out.println("File Created");
 
 
		} 
 
		catch (JAXBException e) 
		{
			e.printStackTrace();
		}
 
	}
 
}

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <employee>
        <emp_id>125</emp_id>
        <emp_name>bhavesh</emp_name>
        <salary>50221.0</salary>
    </employee>
    <employee>
        <emp_id>124</emp_id>
        <emp_name>abhi</emp_name>
        <salary>50200.0</salary>
    </employee>
</company>
File Created

You can see file created

Unmarshalling Example:


Unmarshalling.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.spy.javaforlearn;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
 
public class Unmarshalling 
{
 
	public static void main(String[] args) 
	{
 
 
		try 
		{
 
			JAXBContext context=JAXBContext.newInstance(Company.class);
 
			Unmarshaller unmarshaller=context.createUnmarshaller();
 
			File file=new File("C:/workspace/JavaJAXB/Target/company.xml");
 
			try 
			{
 
				Company cmp=(Company)unmarshaller.unmarshal(file);
 
				System.out.println("Company name: "+cmp.getCname());
 
				List<Emp_info> emp_list=cmp.getList_emp();
 
 
				for (Emp_info list : emp_list) 
				{
 
					System.out.println("Employee name: "+list.employee_name);
					System.out.println("Employee id: "+list.emp_id);
					System.out.println("Employee sal: "+list.sal);
 
					System.out.println("************");
				}
 
 
	    	} 
 
 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
 
		} 
 
 
	catch (JAXBException e)
	{
		e.printStackTrace();
	}
 
	}
 
}

output:

1
2
3
4
5
6
7
8
9
10
 
Company name: Ernst and Young LLP
Employee name: bhavesh
Employee id: 125
Employee sal: 50221.0
************
Employee name: abhi
Employee id: 124
Employee sal: 50200.0
************
  • Debabrata

    Nice article… Thank you