将XML节点转换成Map
private static Map<Object, Object> convertNodeToMap(
Iterator<Element> modelIt, Object parentObj) {
Map<Object, Object> beanMap = new HashMap<Object, Object>();
if (modelIt != null) {// 表目录
// Iterator<Element> modelIt = modelEl.elementIterator();
while (modelIt.hasNext()) {
Element propertyEl = modelIt.next();
String name = propertyEl.attributeValue("name");// 属情
String value = propertyEl.attributeValue("value");// 值
String foreign_key = propertyEl.attributeValue("foreign-key");
String today_value = propertyEl.attributeValue("today-value");
String date_format = propertyEl.attributeValue("date-format");
if (date_format != null) {
df.applyPattern(date_format);
}
java.lang.Object valueObj = null;
if (foreign_key != null && foreign_key.equals("true")) {
valueObj = parentObj;
} else if (today_value != null && today_value.equals("true")) {
valueObj = df.format(new Date());
} else {
valueObj = value;
}
beanMap.put(name, valueObj);
}
}
return beanMap;
}
2。将map转换成bean
// 将Map转化为javaBean
private static <T> T convertMapToBean(Class<T> type, Map<Object, Object> map)
throws IntrospectionException, InstantiationException,
IllegalAccessException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
T t = type.newInstance(); // 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
String value = ConvertUtils.convert(map.get(propertyName));
Object[] args = new Object[1];
try {
args[0] = df.parse(value);
} catch (ParseException e) {
args[0] = ConvertUtils.convert(value,
descriptor.getPropertyType());
}
try {
descriptor.getWriteMethod().invoke(t, args);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return t;
}
3.// 将javaBean 转化为 Map
private static <T> Map<Object, Object> convertBeanToMap(Object bean)
throws IntrospectionException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
try {
Object result = readMethod.invoke(bean, new Object[0]);
returnMap.put(propertyName, result);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.debug("解析方法名:" + readMethod + ",有误!");
// e.printStackTrace();
}
}
}
return returnMap;
}
// 将Map转化为javaBean
private static <T> T convertMapToBean(Class<T> type, Map<Object, Object> map)
throws IntrospectionException, InstantiationException,
IllegalAccessException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
T t = type.newInstance(); // 创建 JavaBean 对象
// 给 JavaBean 对象的属性赋值
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
String value = ConvertUtils.convert(map.get(propertyName));
Object[] args = new Object[1];
try {
args[0] = df.parse(value);
} catch (ParseException e) {
args[0] = ConvertUtils.convert(value,
descriptor.getPropertyType());
}
try {
descriptor.getWriteMethod().invoke(t, args);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return t;
}
}