编程语言
首页 > 编程语言> > JAVA基础学习博客014

JAVA基础学习博客014

作者:互联网

文章目录


学习内容(2020 11/25)

xml解析工具类、线程的初步了解

一、xml解析工具类

package xmlParse;

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 XMLParseDemo {

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

	/**
	 * 1.
	 * 根据用户指定的xml文件路径进行解析
	 * @param xmlPath
	 * 要解析的xml文件路径
	 * @return
	 * 返回解析后的document对象
	 * @throws FileNotFoundException
	 * 当路径找不到事出现该异常
	 * @throws DocumentException
	 * 文件解析出现失败时
	 */
	public static Document newInstence(String xmlPath) throws FileNotFoundException, DocumentException {
		SAXReader reader = new SAXReader();	
		Document document = reader.read(new FileInputStream(xmlPath));
		return document;
	}
	/**
	 * 2.
	 * 将xml解析后的ID和class存储到map集合中
	 * @param xmlPath
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public static void parseInMap(String xmlPath) throws FileNotFoundException, DocumentException {
		Document document = newInstence(xmlPath);
		//获取文件的xml根节点
		Element rootelement = document.getRootElement();
		//找到其中的子节点
		List<Element>elementList = rootelement.elements();
		//遍历节点集合拿到的单独的节点对象(考虑到数据可能为空,此时不存在子节点,因此建议加入一个非空判断)
		if(elementList != null) {
			for(Element el : elementList){
				//拿到一个个子节点,这个子节点中包含了一些属性
				List<Attribute>attribute = el.attributes();
				//遍历这个子节点的属性集合,获取节点上的属性和值
				String key = null;
				String val = null;
				for(Attribute at : attribute){
					String name = at.getName();
					String value = at.getValue();
					//判断当前获取的是id还是class,将键值对的值存到一个变量中,将这两个变量存入到map中
					if("id".equals(name)) {
						key = value;
					}
					if("class".equals(name)) {
						val = value;
					}
				}
				//将key 、val存入map
				map.put(key, val);
			}
		}
	}
	/**
	 * 3.
	 * 根据用户指定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
			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;
	}
	/**
	 * 4.
	 * 根据指定文件路径返回对象
	 * @param classPath
	 * @return
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws SecurityException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static Object getInstenceByClsPath(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();
			//获取val
			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 = getInstenceByClsPath("beans.Item");
		System.out.println(obj);
	}
}

二、线程的初步了解

线程的开启方式

package thread;

public class ThreadDemo {

	
	public static void main(String[] args) {
//		SubThread sub = new SubThread();
		sub.start();
//		sub.run();
//		int i = 1/0;
		
		SubRun sub = new SubRun();
		Thread thread = new Thread(sub);
		thread.start();
		int i = 1/0;
		
	}
}
class SubRun implements Runnable{
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("hello world");
		}
	}
	
}
class SubThread extends Thread{
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("hello world");
		}
	}
	
}

标签:xml,map,JAVA,String,博客,class,014,import,throws
来源: https://blog.csdn.net/qq_51576943/article/details/110143888