码迷,mamicode.com
首页 > 其他好文 > 详细

Dom4J XML转bean

时间:2017-08-17 00:50:30      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:cheng   ack   [1]   imp   lease   exce   style   import   element   

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

 

Dom4J XML转bean

标签:cheng   ack   [1]   imp   lease   exce   style   import   element   

原文地址:http://www.cnblogs.com/chenglc/p/7376443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!