JAXB 2.0 - Reading and Writing XML in Java
JAXB is Java Architecture for XML Binding helps in converting Java class to XML and vice versa.Thus it helps in processing the XML data in the Java application.Unmarshalling can be defined as reading XML document into Java and Marshalling is writing the Java object to XML.
Features of JAXB 2.0
Follow us: www.facebook.com/javainstance
Features of JAXB 2.0
- JAXB 2.0 supports all W3C XML schema features which was not possible in JAXB 1.0.
- Annotations are available to control Java-to-XML binding unlike JAXB 1.0.
- Validations are included using JAXP 1.3 validation APIs.
- Runtime Libraries are smaller.
- Generated Schema-derived classes are reduced in number.
Creating XML from Java Object -Marshalling -
In this example, we are going to create XML file from the Java Object using JAXB2.0 annotations.
@XmlRootElement - To define the class as root element of XML.We can use it with name argument or without name argument.name argument provides control on element name, by default it take Class name as root element.
@XmlType(propOrder={"departmentId","departmentName","employees"}) -Using this annotation you change order of element in XML file.
@XmlElement(name="Department Id") -To define the XML element.
@XmlElementWrapper(name="Employees") - To produce wrapper XML element around the collections.
Department.java
package com.javainstance;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* @author javainstance
*
*/
@XmlRootElement(name = "Department")
@XmlType(propOrder = { "departmentId", "departmentName", "employees" })
public class Department {
private int departmentId;
private String departmentName;
private List<Employee> employees;
public Department() {
}
public Department(int departmentId, String departmentName, List<Employee> employees) {
super();
this.departmentId = departmentId;
this.departmentName = departmentName;
this.employees = employees;
}
@XmlElement(name = "Department_Id")
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
@XmlElement(name = "Department_Name")
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@XmlElementWrapper(name = "Employees")
@XmlElement(name = "Employee")
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* @author javainstance
*
*/
@XmlRootElement(name = "Department")
@XmlType(propOrder = { "departmentId", "departmentName", "employees" })
public class Department {
private int departmentId;
private String departmentName;
private List<Employee> employees;
public Department() {
}
public Department(int departmentId, String departmentName, List<Employee> employees) {
super();
this.departmentId = departmentId;
this.departmentName = departmentName;
this.employees = employees;
}
@XmlElement(name = "Department_Id")
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
@XmlElement(name = "Department_Name")
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@XmlElementWrapper(name = "Employees")
@XmlElement(name = "Employee")
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Employee.java
package com.javainstance;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author javainstance
*
*/
@XmlRootElement(name = "Employee")
public class Employee {
private int employeeId;
private String employeeName;
private int employeedAge;
public Employee() {
}
public Employee(int employeeId, String employeeName, int employeedAge) {
super();
this.employeeId = employeeId;
this.employeeName = employeeName;
this.employeedAge = employeedAge;
}
@XmlElement(name = "Employee_Id")
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
@XmlElement(name = "Employee_Name")
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
@XmlElement(name = "Employee_Age")
public int getEmployeedAge() {
return employeedAge;
}
public void setEmployeedAge(int employeedAge) {
this.employeedAge = employeedAge;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author javainstance
*
*/
@XmlRootElement(name = "Employee")
public class Employee {
private int employeeId;
private String employeeName;
private int employeedAge;
public Employee() {
}
public Employee(int employeeId, String employeeName, int employeedAge) {
super();
this.employeeId = employeeId;
this.employeeName = employeeName;
this.employeedAge = employeedAge;
}
@XmlElement(name = "Employee_Id")
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
@XmlElement(name = "Employee_Name")
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
@XmlElement(name = "Employee_Age")
public int getEmployeedAge() {
return employeedAge;
}
public void setEmployeedAge(int employeedAge) {
this.employeedAge = employeedAge;
}
}
Writer.java
package com.javainstance;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
/**
* @author javainstance
*
*/
public class Writer {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Department.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Employee e1 = new Employee(1, "Abhay", 12);
Employee e2 = new Employee(2, "Pawan", 32);
Employee e3 = new Employee(3, "Jai", 18);
Employee e4 = new Employee(4, "Rajan", 21);
List<Employee> elist1 = new ArrayList<>();
elist1.add(e1);
elist1.add(e2);
elist1.add(e3);
elist1.add(e4);
Department dept = new Department(1, "HR", elist1);
marshaller.marshal(dept, new FileOutputStream("department.xml"));
System.out.println("XML Generated");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Output File: department.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Department>
<Department Id>1</Department Id>
<Department Name>HR</Department Name>
<Employees>
<Employee>
<Employee Id>1</Employee Id>
<Employee Name>Abhay</Employee Name>
<Employee Age>12</Employee Age>
</Employee>
<Employee>
<Employee Id>2</Employee Id>
<Employee Name>Pawan</Employee Name>
<Employee Age>32</Employee Age>
</Employee>
<Employee>
<Employee Id>3</Employee Id>
<Employee Name>Jai</Employee Name>
<Employee Age>18</Employee Age>
</Employee>
<Employee>
<Employee Id>4</Employee Id>
<Employee Name>Rajan</Employee Name>
<Employee Age>21</Employee Age>
</Employee>
</Employees>
</Department>
<Department>
<Department Id>1</Department Id>
<Department Name>HR</Department Name>
<Employees>
<Employee>
<Employee Id>1</Employee Id>
<Employee Name>Abhay</Employee Name>
<Employee Age>12</Employee Age>
</Employee>
<Employee>
<Employee Id>2</Employee Id>
<Employee Name>Pawan</Employee Name>
<Employee Age>32</Employee Age>
</Employee>
<Employee>
<Employee Id>3</Employee Id>
<Employee Name>Jai</Employee Name>
<Employee Age>18</Employee Age>
</Employee>
<Employee>
<Employee Id>4</Employee Id>
<Employee Name>Rajan</Employee Name>
<Employee Age>21</Employee Age>
</Employee>
</Employees>
</Department>
Give your suggestion in comments.
If you like the post Share with your friends on social network.
Follow us: www.facebook.com/javainstance
6 Different ways to iterate through HashMap
JAXB 2.0 - Reading and Writing XML in Java
Reviewed by JavaInstance
on
2:11:00 AM
Rating:
Reviewed by JavaInstance
on
2:11:00 AM
Rating:

No comments: