網頁

2021/4/20

Java 使用jackson-dataformat-xml 讀取xml檔為物件範例 read xml file to object

Java使用jackson-dataformat-xml將xml檔內容讀取為Java物件範例如下。

例如employee.xml位在Maven專案的src/main/resources,內容如下。

employee.xml

<?xml version="1.0" encoding="UTF-8" ?>
<employee>
    <id>100</id>
    <name>John</name>
    <email>john@abc.com</email>
    <age>29</age>
    <onBoardDate>2021-04-19</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>

xml要轉成的Java物件Employee類別及內層元素的DepartmentManager類別如下,記得要寫getter setters否則轉換時會出現錯誤,

Employee

import java.util.Arrays;
import java.util.Date;

public class Employee {

    private int id;
    private String name;
    private String email;
    private int age;
    private Date onBoardDate;
    private Department department;
    private String[] exts;

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                ", onBoardDate=" + onBoardDate +
                ", department=" + department +
                ", exts=" + Arrays.toString(exts) +
                '}';
    }

    // getters and setters
}

Department

public class Department {

    private int id;
    private String name;
    private Manager manager;

    @Override
    public String toString() {
        return "Department{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", manager=" + manager +
                '}';
    }

    // getters and setters
}

Manager

public class Manager {

    private int id;
    private String name;
    private String email;

    @Override
    public String toString() {
        return "Manager{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    // getters and setters
}

使用XmlMapper物件轉換xml為物件如下。讀取的xml元素值皆為字串,如果元素值要轉成日期Date則呼叫setDateFormat()指定日期格式。

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class Main {

    private static final String XML_PATH = "src/main/resources/employee.xml";


    public static void main(String[] arges) throws IOException {
        
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

        Employee employee = xmlMapper.readValue(new File(XML_PATH), Employee.class);

        System.out.println(employee);

    }
}

印出結果

Employee{id=100, name='John', email='john@abc.com', age=29, onBoardDate=Mon Apr 19 00:00:00 CST 2021, department=Department{id=22, name='Marketing', manager=Manager{id=52, name='Mary', email='mary@abc.com'}}, exts=[201, 202]}

轉換的物件內容如下

Employee{
    id=100, 
    name='John', 
    email='john@abc.com', 
    age=29, 
    onBoardDate=Tue Jan 19 00:04:00 CST 2021, 
    department=Department{
        id=22, 
        name='Marketing', 
        manager=Manager{
            id=52, 
            name='Mary', 
            email='mary@abc.com'
        }}, 
    exts=[201, 202]
}


沒有留言:

張貼留言