Hi everyone ! Through this post we’re going to see how to export an ArrayList of a POJO Object to an XML file.
We have a group of employees in a random company, and we want to save their data in an XML generated file as a backup using a Java Application. Each employee has these fallowing attributes
- Code : a String text that identify each employee in the company
- First name : the first name of the employee
- Last name: the last name of the employee
- Email: the email address of the employee
The logical process is very simple. First, we enumerate the employees we want to export then we write them to a List. Then, we use the same list to export the details of each employee.
We are going to create a new Java project list2xml that export an ArrayList of an Employees to an XML file. First we’ll create these following packages:
– list2xml : root package
– list2xml.model : contains POJO classes
– list2xml.dao : contains Init method of an Employee ArrayList
– list2xml.exporter : contains one class with one method for XML exporting
– list2xml.main: contains one class Main with a main method.
Follow these steps below to create the project:
- Under your IDE, create a new Java Project : “list2xml”
- Add a new package : “list2xml”.
- Under the root package, add a new one : model
- Create a new Java class : Employee
package list2xml.model; import java.io.Serializable; public class Employee implements Serializable { private String Code; private String FirstName; private String LastName; private String Email; public Employee() {} public Employee(String Code, String FirstName, String LastName, String Email) { this.Code=Code; this.FirstName=FirstName; this.LastName=LastName; this.Email=Email; } public String getCode() { return this.Code;} public String getFirstName() { return this.FirstName;} public String getLastName() { return this.LastName;} public String getEmail() {return this.Email;} public void setCode(String Code) {this.Code=Code;} public void setFirstName(String FirstName) {this.FirstName=FirstName;} public void setLastName(String LastName) {this.LastName=LastName;} public void setEmail(String Email) { this.Email=Email;} @Override public String toString() { return this.LastName+ " "+this.FirstName+"("+this.Code+"); } }
- Under the root package, add a new one : dao
- Add a new interface: IEmployeeDAO
package list2xml.dao; import java.util.List; import list2xml.model.Employee; public interface IEmployeeDAO { public List InitEmployees(); }
- Add an implementation class : EmployeeImplementation:
package list2xml.dao; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import list2xml.model.Employee; public class EmployeeImplementation implements Serializable, IEmployeeDAO{ @Override public List InitEmployees() { List l=new ArrayList<>(); l.add(new Employee("EMP-022019", "Jane", "ADAMS", "jane.adams@acme.com")); l.add(new Employee("EMP-072016", "John", "ADAMS", "john.adams@acme.com")); return l; }}
This class implements Serializable and IEmployeeDAO interfaces and has 1 override method : InitEmployees that initialize a List of Employees with random details.
- On the root package, add a new one : exporter
- Create a new Java class : EmployeeXmlExporter
package list2xml.exporter; import java.io.FileWriter; import java.io.IOException; import java.util.Date; import java.util.List; import list2xml.dao.EmployeeImplementation; import list2xml.dao.IEmployeeDAO; import list2xml.model.Employee; public class EmployeeXmlExporter { IEmployeeDAO employeeDAO=new EmployeeImplementation(); final String FIELD_CODE="CODE"; final String FIELD_FIRST_NAME="FIRSTNAME"; final String FIELD_LAST_NAME="LASTNAME"; final String FIELD_EMAIL="EMAIL"; public EmployeeXmlExporter() {} public void Export2Xml(List l) throws IOException { FileWriter fw=new FileWriter("employees.xml"); StringBuilder sb=new StringBuilder(); sb.append(""); l=employeeDAO.InitEmployees(); for(Employee emp : l) { sb.append(""); sb.append("<"+FIELD_CODE+">"+emp.getCode()+"</"+FIELD_CODE+">"); sb.append("<"+FIELD_FIRST_NAME+">"+emp.getFirstName()+"</"+FIELD_FIRST_NAME+">"); sb.append("<"+FIELD_LAST_NAME+">"+emp.getLastName()+"</"+FIELD_LAST_NAME+">"); sb.append("<"+FIELD_EMAIL+">"+emp.getEmail()+"</"+FIELD_EMAIL+">"); sb.append(""); } sb.append(""); fw.write(sb.toString()); fw.flush(); fw.close(); }}
This class has one default constructor, one instance of EmployeeImplementation class and the method Export2Xml that export a given ArrayList of Employees to an XML file.
- Under the root package, create a new package : main
- Create under the same package a new class : Main
package list2xml.main; import java.io.IOException; import list2xml.dao.EmployeeImplementation; import list2xml.dao.IEmployeeDAO; import list2xml.exporter.EmployeeXmlExporter; public class Main { public static void main(String[] a) { IEmployeeDAO employeeDAO=new EmployeeImplementation(); EmployeeXmlExporter employeeXML=new EmployeeXmlExporter(); try { employeeXML.Export2Xml(employeeDAO.InitEmployees()); } catch(IOException ex) { ex.printStackTrace(); }}}
This class have the main(String[ ]) method and also, the implementation of the XML export process.
- GitHub repo: https://github.com/MichkaDaCoder/list2xml
- YouTube link :
Any remarks ? Write it below
Thank you !