XML解析类 package com.supermap.services.components.tilecache.convert; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class XMLUtil { /** * 得到第一个非文本的节点 * * @param node * @return */ public static Node getFirstNode(Node node) { NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) { Node childNode = nodelist.item(i); if (childNode instanceof Text) { continue; } return childNode; } return null; } /** * 得到节点下Tag为name的节点 * * @param node * @param name * @return */ public static Node getNodeByTagName(Node node, String name) { Element elem = (Element) node; return elem.getElementsByTagName(name).item(0); } /** * 得到节点下Tag为name的节点集合 * * @param node * @param name * @return 节点集合 */ public static ListgetNodesByTagName(Node node, String name) { Element elem = (Element) node; NodeList nodelist = elem.getElementsByTagName(name); List result = new ArrayList (); for (int i = 0; i < nodelist.getLength(); i++) { result.add(nodelist.item(i)); } return result; } /** * 判断节点是否为文本节点 string 就是文本节点 * * @param node * @return */ public static Boolean isTextNode(Node node) { NodeList childs = node.getChildNodes(); if (childs.getLength() == 1) { Node child = childs.item(0); if (child instanceof Text) { return true; } } return false; } /** * 节点非文本节点的集合 * * @return */ public static List getChildsNodes(Node node) { NodeList nodelist = node.getChildNodes(); List result = new ArrayList (); for (int i = 0; i < nodelist.getLength(); i++) { Node child = nodelist.item(i); if (child instanceof Text) { continue; } result.add(child); } return result; } @SuppressWarnings("unchecked") /** * 把node转成type类型的对象 * @param node * @param type * @return */ public static T nodeToObject(Node node, Class> type) { Object obj = null; if (type.isArray()) {// 考虑数组 Class> itemType = type.getComponentType();//级数元素类型 List childs = getChildsNodes(node); Object array= Array.newInstance(itemType, childs.size()); for(int i =0;i propType = ReflectionUtil.getPropertyType(obj, nodeName); if (propType != null) { Object childValue = nodeToObject(child, propType); ReflectionUtil.setPropertyValue(obj, nodeName, childValue); } } } catch (Exception ex) { ex.printStackTrace(); } } return (T) obj; } }
==============================================
反射类 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()); } }