Employee.java POJO Class
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"));
}
}
Output : EmployeeFile.xml generated
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee employeeCode="11">
<designation>DBA</designation>
<name>David Sherrif</name>
<salary>50000.0</salary>
</employee>
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 static void main(String[] args) throws JAXBException {
// TODO Auto-generated method stub
System.out.println("creating JAXBContext ");
File employeeFile = new File("EmployeeFile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
System.out.println("creating JAXB unmarshel object");
Unmarshaller jaxbUnmarshellObject = jaxbContext.createUnmarshaller();
System.out.println("performing unmarshel of XML employee file to read employee");
Employee emp1 = (Employee)jaxbUnmarshellObject.unmarshal(employeeFile);
System.out.println("Employee File info ::"+emp1);
}
}
Output
