您的位置:首页 > 其它

使用dom4j将xml文件转换成bean

2016-02-03 13:32 281 查看
package com.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class ObjectFromXML {

public static String xmlInfoByNode(String xmlStr, String node) throws DocumentException {
Document doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
Element recode = root.element(node);
if(StringUtils.isEmpty(xmlStr)) {
return null;
}
return recode.getTextTrim();
}

private static Object xmlStrToBean(String xmlStr, Class<?> clazz) {
Object obj = null;
try {
Map<String, Object> map = xmlStrToMap(xmlStr);
obj = mapToBean(map, clazz);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}

private static Map<String, Object> xmlStrToMap(String xmlStr) throws Exception {
if(StringUtils.isEmpty(xmlStr)) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
Document doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
List<Element> children = root.elements();
if(children != null && children.size() > 0) {
for(int i = 0; i < children.size(); i++) {
Element child = (Element)children.get(i);
map.put(child.getName(), child.getTextTrim());
}
}
return map;
}

private static Object mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
Object obj = clazz.newInstance();
if(map != null && map.size() > 0) {
for(Map.Entry<String, Object> entry : map.entrySet()) {
String propertyName = entry.getKey();
Object value = entry.getValue();
String setMethodName = "set"
+ propertyName.substring(0, 1).toUpperCase()
+ propertyName.substring(1);
Field field = getClassField(clazz, propertyName);
Class<?> fieldTypeClass = field.getType();
value = convertValType(value, fieldTypeClass);
clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
}
}
return obj;
}

<span style="white-space:pre">	</span>//判断bean中字段的属性
private static Object convertValType(Object value, Class<?> fieldTypeClass) {
Object retVal = null;
if(Long.class.getName().equals(fieldTypeClass.getName())
|| long.class.getName().equals(fieldTypeClass.getName())) {
retVal = Long.parseLong(value.toString());
} else if(Integer.class.getName().equals(fieldTypeClass.getName())
|| int.class.getName().equals(fieldTypeClass.getName())) {
retVal = Integer.parseInt(value.toString());
} else if(Float.class.getName().equals(fieldTypeClass.getName())
|| float.class.getName().equals(fieldTypeClass.getName())) {
retVal = Float.parseFloat(value.toString());
} else if(Double.class.getName().equals(fieldTypeClass.getName())
|| double.class.getName().equals(fieldTypeClass.getName())) {
retVal = Double.parseDouble(value.toString());
} else {
retVal = value;
}
return retVal;
}

<span style="white-space:pre">	</span>//利用反射得到累的属性
private static Field getClassField(Class<?> clazz, String fieldName) {
if( Object.class.getName().equals(clazz.getName())) {
return null;
}
Field []declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
if (field.getName().equals(fieldName)) {
return field;
}
}

Class<?> superClass = clazz.getSuperclass();
if(superClass != null) {
return getClassField(superClass, fieldName);
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: