加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Object和xml互转

发布时间:2020-12-16 08:02:44 所属栏目:百科 来源:网络整理
导读:import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * Object和xml互转 **/ pub
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/** * Object和xml互转 **/
public class XmlUtil {
    /** * xml文档Document转对象 * * @param document * @param clazz * @return */
    public static Object getObject(Document document,Class<?> clazz) {
        Object obj = null;
        //获取根节点
        Element root = document.getRootElement();
        try {
            obj = clazz.newInstance();//创建对象
            // 获取实体类的所有属性,返回Field数组
            Field[] field = clazz.getDeclaredFields();
            List<Element> properties = root.elements();
            for (Element pro : properties) {
                String propertyname = pro.getName();
                String propertyvalue = pro.getText();

                for (Field field1 : field) {
                    if (field1.getName().equals(propertyname)) {
                        //获取属性名(首字母大写)
                        String mName = propertyname.substring(0,1).toUpperCase() + propertyname.substring(1);
                        Method m = obj.getClass().getMethod("set" + mName,String.class);
                        m.invoke(obj,propertyvalue);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    /** * xml字符串转对象 * * @param xmlString * @param clazz * @return */
    public static Object getObject(String xmlString,Class<?> clazz) throws Exception {
        Document document = null;
        try {
            document = DocumentHelper.parseText(xmlString);
        } catch (DocumentException e) {
            throw new Exception("获取Document异常" + xmlString);
        }      //获取根节点
        return getObject(document,clazz);
    }

    /** * 对象转xml文件 * * @param b * @return */
    public static Document getDocument(Object b) {
        Document document = DocumentHelper.createDocument();
        try {
            // 创建根节点元素
            Element root = document.addElement(b.getClass().getSimpleName());
            Field[] field = b.getClass().getDeclaredFields(); // 获取实体类b的所有属性,返回Field数组
            for (int j = 0; j < field.length; j++) { // 遍历所有有属性
                String name = field[j].getName(); // 获取属属性的名字
                if (!name.equals("serialVersionUID")) {//去除串行化序列属性
                    String methodName = name.substring(0,1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
                    Method m = b.getClass().getMethod("get" + methodName);
                    //System.out.println("属性get方法返回值类型:" + m.getReturnType());
                    String propertievalue = (String) m.invoke(b);// 获取属性值
                    Element propertie = root.addElement(name);
                    if(propertievalue == null){
                        propertie.setText("");
                    }else{
                        propertie.setText(propertievalue);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return document;
    }

    /** * 对象转xml格式的字符串 * * @param b * @return */
    public static String getXmlString(Object b) {
        return getDocument(b).asXML();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读