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 {
private static Map<String,Object> map = new HashMap<>();
public static Document newInstance(String xmlPath) throws FileNotFoundException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new FileInputStream(xmlPath));
return document;
}
public static void parseInMap(String xmlPath) throws FileNotFoundException, DocumentException {
Document document = newInstance(xmlPath);
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();
if("id".equals(keyName)) {
key = keyVal;
}
if("class".equals(keyName)) {
value = keyVal;
}
}
map.put(key, value);
}
}
}
public static Object getInstanceById(String id) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
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();
String idVal = entry.getKey();
if(id.equals(idVal)) {
String className = (String) entry.getValue();
Class cls = Class.forName(className);
Constructor cons = cls.getConstructor();
return cons.newInstance();
}
}
return null;
}
public static Object getInstanceByClsPath(String classPath) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
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();
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);
}
}