将xml的model 节点属性转化为Map , 再将Map 转为javaBen

[/code]将XML节点转换成Map
[code="java"]
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
[code]
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;
}

}

[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值