dom4j java对象和xml互相转化

首先导入dom4j(maven)



   dom4j

   dom4j
   1.6.1


代码

测试实体BankData

public class BankData {
private String id;// 主键
private String bizTime; // 时间
private String idcarrier; // 标识
private String organCode; // 组织编码
private String cardType; // 类型
public BankData() {
}


/**
* 
* @param id
* @param bizTime
* @param idcarrier
* @param organCode
* @param cardType
*/
public BankData(String id, String bizTime, String idcarrier,
String organCode, String cardType) {
super();
this.id = id;
this.bizTime = bizTime;
this.idcarrier = idcarrier;
this.organCode = organCode;
this.cardType = cardType;
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}


public String getBizTime() {
return bizTime;
}


public void setBizTime(String time) {
this.bizTime=time;
}


public String getIdcarrier() {
return idcarrier;
}


public void setIdcarrier(String idcarrier) {
this.idcarrier = idcarrier;
}


public String getOrganCode() {
return organCode;
}


public void setOrganCode(String organCode) {
this.organCode = organCode;
}


public String getCardType() {
return cardType;
}


public void setCardType(String cardType) {
this.cardType = cardType;
}


public String toJSON() {
return "{\"id\":\"" + id + "\",\"bizTime\":\"" + bizTime
+ "\",\"idcarrier\":\"" + idcarrier + "\",\"organCode\":\""
+ organCode + "\",\"cardType\":\"" + cardType + "\"}";
}


@Override
public String toString() {
return "BankData [id=" + id + ", bizTime=" + bizTime + ", idcarrier="
+ idcarrier + ", organCode=" + organCode + ", cardType="
+ cardType + "]";
}

}

工具类(含测试方法)XmlUtil



package com.test;

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;

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();//创建对象
List properties=root.elements();
for(Element pro:properties){
//获取属性名(首字母大写)
String propertyname=pro.getName();
String propertyvalue=pro.getText();
Method m = obj.getClass().getMethod("set"+propertyname,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) {
Document document=null;
try {
document = DocumentHelper.parseText(xmlString);
} catch (DocumentException e) {
throw new FileException("获取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")) {//去除串行化序列属性
name = name.substring(0, 1).toUpperCase()
+ name.substring(1); // 将属性的首字符大写,方便构造get,set方法
Method m = b.getClass().getMethod("get" + name);
// System.out.println("属性get方法返回值类型:" + m.getReturnType());
String propertievalue = (String) m.invoke(b);// 获取属性值
Element propertie = root.addElement(name);
propertie.setText(propertievalue);
}
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();


}

return document;
}

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

public static void main(String[] args) {
BankData bank=new BankData("1233","2017-06-13 10:24:31","EEEEEE=","000091","0021");//创建一个任意实体对象
System.out.println(XmlUtil.getXmlString(bank));//调用方法把对象转xml格式
System.out.println(XmlUtil.getObject(XmlUtil.getXmlString(bank), BankData.class));//调用getXmlString()方法把xml格式重新转换为对象
}

}

你可能感兴趣的:(java)