XML has been way of communication between systems, modules or programs etc. If it is the case, there is need to have framework that eases out reading from and writing to XML format and Java plain objects. It is addressed by JAXB specification.
There are multiple ways by which to and fro transformation between XML and Plain Java objects can achieved.
Using DOM Parser : This approch will read input XML stream and convert the in-memory tree representation of XML. XML DOM tree can be navigated through set of APIs
Using SAX Parser : This is event based framework to navigate through XML stream. We need to provider listener implmentation for SAX events in order to read/write XML data.
XML JAXB Annotations : Annotations can help to transform XML stream into Plain Java objects and vice-versa. Annotations are placed on properties or accessor getter methods of Plain Java classes to achieve the mapping.
import javax.xml.bind.annotation.XmlElement;
/**
*
*/
/**
* @author Owner
*
*/
public class Employee {
/**
*
*/
public Employee() {
// TODO Auto-generated constructor stub
}
String name, designation;
int employeeCode;
float salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public int getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(int employeeCode) {
this.employeeCode = employeeCode;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public Employee(String name, String designation, int employeeCode, float salary){
this.name=name;
this.designation=designation;
this.employeeCode=employeeCode;
this.salary=salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", designation=" + designation + ", employeeCode=" + employeeCode
+ ", salary=" + salary + "]"+"\n";
}
}
Department.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Department {
public Department() {
// TODO Auto-generated constructor stub
super();
employeeList = new ArrayList();
}
String departmentName;
String departmentCode;
List employeeList;
@XmlAttribute
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@XmlAttribute
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
@XmlElement(name="Employee")
public List getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List employeeList) {
this.employeeList = employeeList;
}
public Department(String departmentCode, String departmentName, List employeeList){
this.departmentCode=departmentCode;
this.departmentName=departmentName;
this.employeeList=employeeList;
}
public void addEmployee(Employee employee){
this.employeeList.add(employee);
}
@Override
public String toString() {
return "Department [departmentName=" + departmentName + ", departmentCode=" + departmentCode + ", employeeList="
+ employeeList + "]";
}
}
TransformObjectToXML.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TransformObjectToXML {
public TransformObjectToXML() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws JAXBException, FileNotFoundException {
// TODO Auto-generated method stub
//Creating object of Department
Department aerospace = new Department("222", "AEROSPACE", new ArrayList());
//add employeeObjects to departments
Employee david = new Employee("David Sherrif", "DBA", 11, 50000);
Employee Kevin = new Employee("Kevin Taylor","QA",22,30000);
Employee rudolf = new Employee("Rudolf Kane","Dev",33,70000);
Employee thomas = new Employee("Thomas Drake","MANAGER",55,90000);
//Adding employees into department
aerospace.addEmployee(david);
aerospace.addEmployee(Kevin);
aerospace.addEmployee(rudolf);
aerospace.addEmployee(thomas);
JAXBContext jaxbContext = JAXBContext.newInstance(Department.class);
Marshaller marshellObject = jaxbContext.createMarshaller();
marshellObject.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshellObject.marshal(aerospace, new FileOutputStream("DepartmentFile.xml"));
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
String name, designation;
int employeeCode;
float salary;
public Employee(){
}
public Employee(String name, String designation, int employeeCode, float salary){
super();
this.name=name;
this.designation=designation;
this.employeeCode=employeeCode;
this.salary=salary;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@XmlAttribute
public int getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(int employeeCode) {
this.employeeCode = employeeCode;
}
@XmlElement
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", designation=" + designation + ", employeeCode=" + employeeCode
+ ", salary=" + salary + "]";
}
}
TransformObjectToXml.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TransformObjectToXml {
public static void main(String[] args) throws JAXBException, FileNotFoundException {
// TODO Auto-generated method stub
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller marshellObject = jaxbContext.createMarshaller();
marshellObject.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println("creating object for person david");
Employee david = new Employee("David Sherrif","DBA",11,50000);
System.out.println("David info ::"+david);
System.out.println("Writing to xml file David information");
marshellObject.marshal(david, new FileOutputStream("EmployeeFile.xml"));
}
}