Giving a precise answer without the implementation and annotation of your Employee class is hard to do.
Therefore I wrote a small example which is highly related to yours. I hope this is helpful :)
Hint: The important part is writing the XML-Annotations.
Main.class
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws Exception {
JAXBContext contextObj = JAXBContext.newInstance(Employees.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
try {
Employees employees = new Employees();
Employee emp1 = new Employee(1, "Vimal Jaiswal", 50000);
Employee emp2 = new Employee(2, "Kamal", 40000);
employees.getEmployees().add(emp1);
employees.getEmployees().add(emp2);
marshallerObj.marshal(employees, new FileOutputStream(
"W:\\employee.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Employees.class
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employees {
private List<Employee> employees;
public Employees() {
employees = new ArrayList<Employee>();
}
@XmlElement
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Employee.class
import javax.xml.bind.annotation.XmlElement;
public class Employee {
private int id;
private String name;
private int salary;
public Employee(int id, String name, int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
The resulting XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees>
<employees>
<id>1</id>
<name>Vimal Jaiswal</name>
<salary>50000</salary>
</employees>
<employees>
<id>2</id>
<name>Kamal</name>
<salary>40000</salary>
</employees>
</employees>