反射獲取數據後用XML文件進行保存讀取操作

反射+XML文件保存讀取操作

代碼展示:

package 包名;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Anonymous
 * @time: 2020/3/6 16:46
 */
public class MainProject {
    public static void main(String[] args)
            throws IllegalAccessException, DocumentException, NoSuchMethodException, InvocationTargetException, IOException {
        ArrayList<Student> list = new ArrayList<>();

        readDataFromXML(list);

        for (Student student : list) {
            System.out.println(student);
        }
    }

    public static void saveDateToXML(ArrayList<Student> list) throws IllegalAccessException, IOException {
        // 創建XML文件對應Document對象
        Document document = DocumentHelper.createDocument();

        // 明確根節點
        Element root = document.addElement("students");

        // 獲取所有的成員變量Field對象
        Field[] declaredFields = Student.class.getDeclaredFields();

        // 循環遍歷Student ArrayList集合
        for (Student student : list) {
            // 每一個Student對象都要對應一個Student節點
            Element element = root.addElement("student");

            // 遍歷所有的Field成員變量
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);

                // id存儲到Student節點中的屬性中
                if ("id".equals(declaredField.getName())) {
                    // 所有的數據都是在String類型處理
                    element.addAttribute("id", declaredField.get(student) + "");
                } else {
                    // declaredField.getName() 成員變量名字 declaredField.get(student) 對應數據
                    element.addElement(declaredField.getName()).addText(declaredField.get(student) + "");
                }
            }
        }

        // 字符流對象+Document對象的write方法寫入XML信息到文件中
        FileWriter fileWriter = new FileWriter("./xml/student.xml");
        document.write(fileWriter);
        fileWriter.close();
    }

    public static void readDataFromXML(ArrayList<Student> list)
            throws DocumentException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Document document = new SAXReader().read(new File("./xml/student.xml"));

        // 找出當前XML文件中的所有student節點
        List list1 = document.selectNodes("//student");

        // 得到當前Student類內的所有成員變量對象,注意使用暴力反射
        Field[] declaredFields = Student.class.getDeclaredFields();

        /*
        String字符串問題
            name String getText
            gender String getText

            id Integer Attribute ==> Integer
            age Integer getText ==> Integer
         */

        // 遍歷所有的Student節點
        for (Object o : list1) {
            // Student節點對象
            Element element = (Element) o;
            Student student = new Student();

            // 成員變量Field數組遍歷
            for (Field declaredField : declaredFields) {
                // 給予暴力反射操作成員變量權限
                declaredField.setAccessible(true);

                // 獲取當前成員變量的數據類型
                Class<?> type = declaredField.getType();

                // 如果數據類型是String類型
                if (type.equals(String.class)) {
                    // String
                    declaredField.set(student, element.element(declaredField.getName()).getText());
                } else if (type.equals(Integer.class)) {
                    // Integer類型
                    // 獲取Integer類型中的valueOf方法
                    Method valueOf = type.getMethod("valueOf", String.class);

                    if ("id".equals(declaredField.getName())) {
                        /*
                        id是在student節點屬性中,從屬性中獲取對應是數據,使用valueOf方法轉換成對應的Integer類型
                         */
                        declaredField.set(student, valueOf.invoke(student, element.attributeValue("id")));
                    } else {
                        /*
                        非ID數據,從Student指定名字的子節點下獲取,指定名字和成員變量名字一直,同樣需要轉換一下
                         */
                        declaredField.set(student,valueOf.invoke(student, element.element(declaredField.getName()).getText()));
                    }
                }
            }

            list.add(student);
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章