Java XML解析,,Node直接转为对象。考虑了一般的类,简单类型,数组,还未考虑List,Map...


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 List<Node> getNodesByTagName(Node node, String name) {
Element elem = (Element) node;
NodeList nodelist = elem.getElementsByTagName(name);
List<Node> result = new ArrayList<Node>();
for (int i = 0; i < nodelist.getLength(); i++) {
result.add(nodelist.item(i));
}
return result;
}

/**
* 判断节点是否为文本节点 <a>string</a> 就是文本节点
*
* @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<Node> getChildsNodes(Node node) {
NodeList nodelist = node.getChildNodes();
List<Node> result = new ArrayList<Node>();
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> T nodeToObject(Node node, Class<?> type) {
Object obj = null;
if (type.isArray()) {// 考虑数组
Class<?> itemType = type.getComponentType();//级数元素类型
List<Node> childs = getChildsNodes(node);
Object array= Array.newInstance(itemType, childs.size());
for(int i =0;i<childs.size();i++){
Node childNode = childs.get(i);
Object childValue = nodeToObject(childNode,itemType);
Array.set(array, i, childValue);
}
return (T) array;
}
if(type.isPrimitive()){//如果是简单类型
return (T) ReflectionUtil.getValue(type, node.getTextContent());
}
//list类型
try {
obj = type.newInstance();//一般意义的类了
} catch (Exception e) {
e.printStackTrace();
return (T) obj;
}
NodeList childs = node.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
Node child = childs.item(i);
if (child instanceof Text) {
continue;
}
String nodeName = child.getNodeName();
try {
if (isTextNode(child)) {// 如果是文本类的
ReflectionUtil.setPropertyValue(obj, nodeName,
child.getTextContent());
} else {
Class<?> 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());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值