Dom4J XML转bean

Posted 路迢迢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        }
    }
}

 

以上是关于Dom4J XML转bean的主要内容,如果未能解决你的问题,请参考以下文章

XML 转 JSON

xml 转map dom4j

基于Dom4j实现javaBean转xml的功能

java字符串利用dom4j转 xml 且遍历

带命名空间的XML的dom4j应用<转>

dom4j 最常用最简单的用法(转)