Java基础综合案例

Java基础综合案例

1 题目
  • 需求:提供对应的方法,能够让用户可以根据ID或者文件路径来获取对应的对象
  • 思路:解析给定的xml文件,将解析后的ID和class的值存储起来(使用键值对),根据用户给定的条件(ID,路径名)返回对应的class对象
2 步骤:
  • 1、解析用户指定的xml文件,返回解析后的文档(document)对象
  • 2、创建Map集合,用于存储解析后的ID及class的值
  • 3、定义一个功能–根据ID返回对象
  • 4、定义一个功能–根据路径名返回对象
3.具体程序实现
package cn.yunhe.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class XMLParseUtil {
	
	/**
	 * 用于存储xml解析后的ID及class值
	 * key:ID对应的值
	 * value:class对应的值
	 */
	private static Map<String,Object> map = new HashMap<>();

	/**
	 * 根据用户指定的xml文件路径进行解析
	 * @param xmlPath
	 * 	要解析的xml文件对应的路径
	 * @return
	 * 	返回解析后对应的document对象
	 * @throws FileNotFoundException
	 * 	当路径找不到时出现该异常
	 * @throws DocumentException
	 * 	文件解析失败时出现该异常
	 */
	public static Document newInstance(String xmlPath) throws FileNotFoundException, DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new FileInputStream(xmlPath));
		return document;
	}
	
	/**
	 * 将xml解析后的ID及class值存储到Map集合中
	 * @param xmlPath
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public static void parseInMap(String xmlPath) throws FileNotFoundException, DocumentException {
		Document document = newInstance(xmlPath);
		//获取xml文件的根节点
		Element rootElement = document.getRootElement();
		//找到其中的子节点
		List<Element> elementList = rootElement.elements();
		//遍历集合拿到单独的节点对象(考虑到数据可能为空,此时就不存在子节点)
		if(null != elementList) {
			for(Element el : elementList) {
				//需要使用节点对应的属性对象
				List<Attribute> attrList = el.attributes();
				//遍历属性集合,获取节点上对应的属性及值
				String key = null;
				String value = null;
				for(Attribute attr : attrList) {
					String keyName = attr.getName();
					String keyVal = attr.getValue();
					//判断当前是key还是value
					if("id".equals(keyName)) {
						key = keyVal;
					}
					if("class".equals(keyName)) {
						value = keyVal;
					}
				}
				//将keyvalue存储到集合中
				map.put(key, value);
			}
		}
	}
	
	/**
	 * 根据用户指定的ID获取对象
	 * @param id
	 * @return
	 * 	如果ID存在就返回Object对象,不存在就返回null
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static Object getInstanceById(String id) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//遍历Map集合,判断集合中是否存在对应的ID
		Set<Map.Entry<String,Object>> set = map.entrySet();
		//获取迭代器
		Iterator<Map.Entry<String,Object>> it = set.iterator();
		//开始迭代
		while(it.hasNext()) {
			Map.Entry<String,Object> entry = it.next();
			//获取key及value
			String idVal = entry.getKey();
			if(id.equals(idVal)) {//指定的ID存在
				//返回对应的对象(通过反射处理)
				String className = (String) entry.getValue();
				//开始反射
				Class cls = Class.forName(className);
				//反射要求必须要有一个无参构造器
				Constructor cons = cls.getConstructor();
				//返回无参构造器对应的对象
				return cons.newInstance();
			}
		}
		return null;
	}
	
	/**
	 * 根据指定的文件路径返回对象
	 * @param classPath
	 * @return
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static Object getInstanceByClsPath(String classPath) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//遍历Map集合,判断集合中是否存在对应的ID
		Set<Map.Entry<String,Object>> set = map.entrySet();
		//获取迭代器
		Iterator<Map.Entry<String,Object>> it = set.iterator();
		//开始迭代
		while(it.hasNext()) {
			Map.Entry<String,Object> entry = it.next();
			//获取value的值
			String val = (String) entry.getValue();
			if(classPath.equals(val)) {
				Class cls = Class.forName(classPath);
				Constructor cons = cls.getConstructor();
				return cons.newInstance();
			}
		}
		return null;
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, FileNotFoundException, DocumentException {
		parseInMap("src/item.xml");
		Object obj = getInstanceByClsPath("cn.yunhe.beans.Item");
		System.out.println(obj);
	}
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值