Java XML解析,,Node直接转为对象。考虑了一般的类,简单类型,数组,还未考虑List,Map
2012-04-23 15:28
751 查看
反射类
package com.supermap.services.components.tilecache.convert; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import com.supermap.services.components.commontypes.OutputFormat; public class ReflectionUtil { /** * 给属性赋值[默认包括了字段] * @param obj * @param proName * @param value * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static void setPropertyValue(Object obj,String proName,Object value) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ BeanInfo beanInfo= Introspector.getBeanInfo(obj.getClass()); for(PropertyDescriptor prop : beanInfo.getPropertyDescriptors()){ if(prop.getName().equals(proName)){ Class<?> propType =prop.getReadMethod().getReturnType(); Object porpvalue = getValue(propType, value); prop.getWriteMethod().invoke(obj, porpvalue); return ; } } for(java.lang.reflect.Field field : obj.getClass().getFields()){ if( field.getName().equals(proName)){ Object filedValue= getValue(field.getType(),value); field.set(obj, filedValue); return ; } } } /** * 得到属性的类别 * @param obj * @param proName * @return * @throws IntrospectionException */ public static Class<?> getPropertyType(Object obj,String proName) throws IntrospectionException{ BeanInfo beanInfo= Introspector.getBeanInfo(obj.getClass()); for(PropertyDescriptor prop : beanInfo.getPropertyDescriptors()){ if(prop.getName().equals(proName)){ return prop.getReadMethod().getReturnType(); } } for(java.lang.reflect.Field field : obj.getClass().getFields()){ if( field.getName().equals(proName)){ return field.getType(); } } return null; } /** * 把obj转成type类型 * @param type * @param obj * @return */ public static Object getValue(Class<?> type,Object obj){ String className = type.getName(); if(obj.getClass() == type){ return obj; } if(type .equals(Double.class) ||className=="double"){ return Double.parseDouble(obj.toString()); } if(type==Float.class||className=="float"){ return Float.parseFloat(obj.toString()); } if(type==Integer.class||className=="int"){ return Integer.parseInt(obj.toString()); } if(type.equals( String.class)||className=="string"){ return obj.toString(); } if(type.equals(Boolean.class)||className=="boolean"){ return Boolean.parseBoolean(obj.toString()); } if(type.isEnum()){ Class<?>[] params = new Class<?>[1]; params[0] = String.class; try { return type.getDeclaredMethod("valueOf", params).invoke(null, obj.toString()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } //if(type.equals(Enum)) return null; } public static void main(String[] argc){ OutputFormat format = OutputFormat.BINARY; //OutputFormat.valueOf(name) //format.valueOf(name) OutputFormat myEnum= (OutputFormat) getValue(format.getClass(),"BINARY"); System.out.println(format.toString()); } }
相关文章推荐
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
- Java 通过反射原理,通过Map转为Java POJO对象,适用于对象包含List,Map,数组等
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- JAVA中将对象转为Map类型
- json(3)---java中对象、数组、list、map转换为json对象
- java类型list、set、数组、map之间的转换
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- 在Java中将数据库查询结果保存为List<Map>类型的JavaScript数组文件
- 参数传递数组 接收 前端使用jquery向后台传递数组类型的参数,java后台直接通过list类型接收,会发现无法取到参数的情况。
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- 在JavaScript中实现类似Java中List<Map>类型的数组
- Java调用存储过程(返回:简单类型、自定义对象、列表数组)
- java中list强转为map类型
- JAVA 集合List,数组,Set,Map,直接的相互转换
- Android JSON数据格式的解析,与简单对象、带泛型的List和Map的转换,以及GSON的使用
- xml,json,map,java对象互相转换解析
- JAVA 集合List,数组,Set,Map,直接的相互转换
- Java中List, Set等转为相应类型的数组的简便方法lamda
- 【Java】使用Java对Xml的解析,并把它的所有二级结点放到MAP中,再把所有的MAP放到list之中
- java如何解析json字符串(List<Map<String,Object>成功的类型)