Dom4J XML转bean
阅读原文时间:2023年07月08日阅读:1

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

}