首先导入dom4j(maven)
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
代码
测试实体BankData
public class BankData {
private String id;
private String bizTime;
private String idcarrier;
private String organCode;
private String cardType;
public BankData() {
}
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<Element> 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;
}
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);
}
public static Document getDocument(Object b) {
Document document = DocumentHelper.createDocument();
try {
Element root = document.addElement(b.getClass().getSimpleName());
Field[] field = b.getClass().getDeclaredFields();
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);
Method m = b.getClass().getMethod("get" + name);
String propertievalue = (String) m.invoke(b);
Element propertie = root.addElement(name);
propertie.setText(propertievalue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
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));
System.out.println(XmlUtil.getObject(XmlUtil.getXmlString(bank), BankData.class));
}
}