• 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;
            }
        }
    }
  • 相关阅读:
    快速排序 C语言实现
    怎样认识女孩?
    看了你就想结婚!)
    C语言面试题大汇总
    VC6.0下载地址
    STL vector 容器介绍
    Beauty of Maths! Amazing!
    自我参考:Java学习的30个目标
    Beauty of Maths! Amazing!
    采用 Java 持久化框架:选择、时机和优缺点?
  • 原文地址:https://www.cnblogs.com/chenglc/p/7376443.html
Copyright © 2020-2023  润新知