With the XML file for acquiring reflection data storage reading operation

Reflex save + XML file read operation

Code shows:

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);
        }

    }
}
Published 19 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41986648/article/details/104757191