zoukankan      html  css  js  c++  java
  • Dom4J XML转bean

    package com.baiwang.bop.utils;
    
    import com.baiwang.bop.client.BopException;
    import org.dom4j.Element;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by ChengLuchao on 2017/8/12.
     */
    public class XmlUtil {
    
        /**
         * Element转list
         *
         * @param root  ontnull
         * @param clazz ontnull
         * @param <T>   ontnull
         * @return bean
         */
        public static <T> List<T> getList(Element root, Class<T> clazz) {
            List<T> list = new ArrayList<>();
            try {
                List<Element> elements = root.elements();
                for (int i = 0; i < elements.size(); i++) {
                    T t = getBean(elements.get(i), clazz);
                    list.add(t);
                }
            } catch (Exception e) {
                throw new BopException(e);
            }
            return list;
        }
    
        /**
         * Element转Bean
         *
         * @param root  ontnull
         * @param clazz ontnull
         * @param <T>   ontnull
         * @return bean
         */
        public static <T> T getBean(Element root, Class<T> clazz) {
            try {
                T t = clazz.newInstance();
                Field[] properties = clazz.getDeclaredFields();
                Method setmeth;
                String fieldType;
                String fieldGenericType;
                String className;
                for (int i = 0; i < properties.length; i++) {
                    fieldType = (properties[i].getType() + "");
                    setmeth = t.getClass().getMethod(
                            "set"
                                    + properties[i].getName().substring(0, 1)
                                    .toUpperCase()
                                    + properties[i].getName().substring(1), properties[i].getType());
                    if ("interface java.util.List".equals(fieldType)) {
                        fieldGenericType = properties[i].getGenericType() + "";
                        String[] sp1 = fieldGenericType.split("<");
                        String[] sp2 = sp1[1].split(">");
                        className = sp2[0];
                        Object listNode = getList(root.element(properties[i].getName()),
                                Class.forName(className));
                        setmeth.invoke(t, listNode);
                    } else {
                        setmeth.invoke(t, root.elementText(properties[i].getName()));
                    }
                }
                return t;
            } catch (Exception e) {
                throw new BopException(e);
            }
        }
    
        /**
         * 判断是否是合法的list
         *
         */
        public static boolean isList(Element root) {
            int type = 0;
            if (root != null) {
                List<Element> elements = root.elements();
                String elementName;
                String elementNameFlag;
                if (elements != null && elements.size() > 0) {
                    elementNameFlag = elements.get(0).getName();
                    for (int i = 1; i < elements.size(); i++) {
                        elementName = elements.get(i).getName();
                        if (elementNameFlag.equals(elementName)) {
                            // 是list
                            type = 1;
                        } else {
                            if (type == 1) {
                                throw new BopException(
                                        "This XML is not in the right format,"
                                        + "please add a parent node for Node of the same name!");
                            } else {
                                elementNameFlag = elementName;
                            }
                        }
                    }
                }
            }
            if (type == 1) {
                return true;
            } else {
                return false;
            }
        }
    }
  • 相关阅读:
    Gogs http和ssh地址显示localhost的问题
    SQL Server从读写频繁的大表中删除大批量数据
    Java Spring Boot: Unable to determine jdbc url from datasource
    NPOI导入Excel日期格式的处理
    手把手教你整合最优雅SSM框架
    JDBC链接数据库MySQL 8.0 Public Key Retrieval is not allowed 错误的解决方法
    Vim 入门教程
    jquery.i18n.properties前端国际化方案
    生产者消费者模式中条件判断是使用while而不是if
    ThreadPoolExecutor 中的 shutdown() 、 shutdownNow() 、 awaitTermination() 的用法和区别
  • 原文地址:https://www.cnblogs.com/chenglc/p/7376443.html
Copyright © 2011-2022 走看看