Tuesday, September 22, 2015

Bài tập IXJ - Object/ Object List to XML File and XML File to Object/ Object List

Trong bài tập này chúng ta sẽ chuyển đổi qua lại giữa XML và List<Object>. Bài tập chuyển từ Object <-> XML

Ví dụ danh sách chứa các Emp như sau:

[sourcecode language="java"]
Emp emp = new Emp();
emp.setCode("A001");
emp.setName("Nguyen Van Mit");
emp.setAddress("Can Tho");
emp.setTel("+8499999999");

Emp emp1 = new Emp();
emp1.setCode("A002");
emp1.setName("Tran Van Cam");
emp1.setAddress("Can Tho");
emp1.setTel("+848888888");

List<Emp> emplist = new ArrayList<Emp>();
emplist.add(emp);
emplist.add(emp1);
[/sourcecode]
Sẽ được chuyển thành file XML có nội dung như sau:
[sourcecode language="xml"]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<emplist>
<emp code="A001">
<address>Can Tho</address>
<name>Nguyen Van Mit</name>
<tel>+8499999999</tel>
</emp>
<emp code="A002">
<address>Can Tho</address>
<name>Tran Van Cam</name>
<tel>+848888888</tel>
</emp>
</emplist>
[/sourcecode]

Để thực thực việc chuyển đổi này chúng ta sử dụng thư viện JAXB của Java.
1. Tạo lớp Emp
[sourcecode language="java"]
package jaxb_object_list;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
//the goc
@XmlRootElement(name = "emp")
public class Emp {
private String code;
private String name;
private String address;
private String tel;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCode() {
return code;
}
@XmlAttribute
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}

@Override
public String toString() {
return "Emp [code:" + code + ", name:" + name + ", address:"
+ address + ", tel:" + tel + "]";
}
}
[/sourcecode]
2. EmpList
[sourcecode language="java"]
package jaxb_object_list;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
// cac thuoc tinh duoc luu duoi dang the
@XmlAccessorType(XmlAccessType.FIELD)
// the goc
@XmlRootElement(name = "emplist")
public class EmpList {
// moi phan tu trong danh sach luu thanh the emp
// cau truc the emp mo ta qua lop Emp
@XmlElement(name = "emp", type = Emp.class)
private List<Emp> emplist = new ArrayList<Emp>();
public EmpList() {}
public EmpList(List<Emp> emplist) {
this.emplist = emplist;
}
public List<Emp> getEmpList() {
return emplist;
}
public void setEmpList(List<Emp> emplist) {
this.emplist = emplist;
}
}
[/sourcecode]

3. Tạo 2 phương thức để chuyển đổi
[sourcecode language="java"]
// chuyen ds doi tuong thanh xml
public static void marshal(List<Emp> emplist, File selectedFile)
throws IOException, JAXBException {
JAXBContext context;
BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(selectedFile));
context = JAXBContext.newInstance(EmpList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new EmpList(emplist), writer);
writer.close();
}

// chuyen XML thanh danh sach doi tuong
public static List<Emp> unmarshal(File importFile) throws JAXBException {
EmpList empList = new EmpList();
JAXBContext context = JAXBContext.newInstance(EmpList.class);
Unmarshaller um = context.createUnmarshaller();
empList = (EmpList) um.unmarshal(importFile);
return empList.getEmpList();
}
[/sourcecode]

4. Test
[sourcecode language="java"]
public static void main(String[] args) {
Emp emp = new Emp();
emp.setCode("A001");
emp.setName("Nguyen Van Mit");
emp.setAddress("Can Tho");
emp.setTel("+8499999999");

Emp emp1 = new Emp();
emp1.setCode("A002");
emp1.setName("Tran Van Cam");
emp1.setAddress("Can Tho");
emp1.setTel("+848888888");

List<Emp> emplist = new ArrayList<Emp>();
emplist.add(emp);
emplist.add(emp1);
//Marshalling: ghi ds doi tuong ra file xml
try {
JAXBXMLHandler.marshal(emplist, new File("src/jaxb_object_list/EmpList.xml"));
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}

try {
// khoi tao ds tu XML
emplist = JAXBXMLHandler.unmarshal(new File("src/jaxb_object_list/EmpList.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
System.out.println(emplist);
}
[/sourcecode]

Tới đây chạy ứng dụng chúng ta sẽ có file xml sinh ra và mẫu in ra của sổ output
[sourcecode language="text"]
[Emp [code:A001, name:Nguyen Van Mit, address:Can Tho, tel:+8499999999], Emp [code:A002, name:Tran Van Cam, address:Can Tho, tel:+848888888]]
[/sourcecode]

Mã nguồn tham khảo tại đây

No comments:

Post a Comment

Translate