首页 / JAVA / Java基础综合案例
Java基础综合案例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java基础综合案例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4825字,纯文字阅读大概需要7分钟。
内容图文
![Java基础综合案例](/upload/InfoBanner/zyjiaocheng/621/198cd3f059e9407a837b21bcc1f8f2f9.jpg)
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);
}
}
内容总结
以上是互联网集市为您收集整理的Java基础综合案例全部内容,希望文章能够帮你解决Java基础综合案例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。