Categories
Java JAVA-XML BINDING

Composite Class to XML

Employee.java

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"));
		
			
	}

}

Output : Generated DepartmentFile.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <department departmentCode="222" departmentName="AEROSPACE"> 
         <Employee> 
                 <designation>DBA</designation> 
                 <employeeCode>11</employeeCode>
                 <name>David Sherrif</name> 
                 <salary>50000.0</salary> 
       </Employee>
      <Employee> 
                 <designation>QA</designation> 
                 <employeeCode>22</employeeCode>
                 <name>Kevin Taylor</name> 
                 <salary>30000.0</salary> 
     </Employee> 
     <Employee>
               <designation>Dev</designation> 
               <employeeCode>33</employeeCode> 
               <name>Rudolf Kane</name> 
               <salary>70000.0</salary> 
    </Employee> 
    <Employee> 
              <designation>MANAGER</designation> 
              <employeeCode>55</employeeCode> 
              <name>Thomas Drake</name> 
              <salary>90000.0</salary> 
   </Employee>
 </department>

TransformXMLToObject.java

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TransformXMLtoObject {

	public TransformXMLtoObject() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) throws JAXBException {
		// TODO Auto-generated method stub
		System.out.println("creating JAXBContext ");
		File departmentFile = new File("DepartmentFile.xml");
		JAXBContext jaxbContext = JAXBContext.newInstance(Department.class);
		
		System.out.println("creating JAXB unmarshel object");
		Unmarshaller jaxbUnmarshellObject = jaxbContext.createUnmarshaller();
		
		System.out.println("performing unmarshel of XML Department file to read employees");
		Department department = (Department)jaxbUnmarshellObject.unmarshal(departmentFile);
		System.out.println("Department File info ::"+department);
		

	}

}

Output

Leave a comment

Design a site like this with WordPress.com
Get started