Java使用JAXB(Java Architecture for XML Binding)將Java物件寫出為xml檔內的範例如下。
以下為要轉成xml檔的Java物件類別。
Employee
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Arrays;
import java.util.Date;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"id", "name", "email", "age", "onBoardDate", "department", "exts"})
@XmlRootElement(name = "employee")
public class Employee {
private int id;
private String name;
private String email;
private int age;
@XmlJavaTypeAdapter(DateAdapter.class)
private Date onBoardDate;
private Department department;
@XmlElementWrapper(name = "exts")
@XmlElement(name = "ext")
private String[] exts;
// getters and setters
}
xml內層元素的對應類別。
Department
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"id", "name", "manager"})
public class Department {
private int id;
private String name;
private Manager manager;
// getters and setters
}
Manager
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"id", "name", "email"})
public class Manager {
private int id;
private String name;
private String email;
// getters and setters
}
如果特定型態如日期Date
要轉成指定格式,則自訂一個實作XmlAdapter
的類別做為@XmlJavaTypeAdapter
的參數並掛在要轉換的欄位上進行轉換。例如下面的DateAdapter
。
DateAdapter
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateAdapter extends XmlAdapter<String, Date> {
private static final String PATTERN = "yyyy-MM-dd";
@Override
public Date unmarshal(String v) throws Exception {
return new SimpleDateFormat(PATTERN).parse(v);
}
@Override
public String marshal(Date v) {
return new SimpleDateFormat(PATTERN).format(v);
}
}
轉換方式如下。
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.util.Date;
public class Main {
private static final String XML_PATH = "src/main/resources/employee.xml";
public static void main(String[] arges) throws JAXBException {
Manager manager = new Manager();
manager.setId(52);
manager.setName("Mary");
manager.setEmail("mary@abc.com");
Department department = new Department();
department.setId(22);
department.setName("Marketing");
department.setManager(manager);
Employee employee = new Employee();
employee.setId(100);
employee.setName("John");
employee.setEmail("john@abc.com");
employee.setAge(29);
employee.setOnBoardDate(new Date());
employee.setDepartment(department);
employee.setExts(new String[]{"201", "202"});
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(employee, new File(XML_PATH));
}
}
執行後會寫出employee.xml
於專案的src/main/resources
(此目錄必須存在)內容如下。
employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><id>100</id><name>John</name><email>john@abc.com</email><age>29</age><onBoardDate>2021-04-27</onBoardDate><department><id>22</id><name>Marketing</name><manager><id>52</id><name>Mary</name><email>mary@abc.com</email></manager></department><exts><ext>201</ext><ext>202</ext></exts></employee>
重新排版整理如下。
employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<id>100</id>
<name>John</name>
<email>john@abc.com</email>
<age>29</age>
<onBoardDate>2021-04-27</onBoardDate>
<department>
<id>22</id>
<name>Marketing</name>
<manager>
<id>52</id>
<name>Mary</name>
<email>mary@abc.com</email>
</manager>
</department>
<exts>
<ext>201</ext>
<ext>202</ext>
</exts>
</employee>
沒有留言:
張貼留言