通过解析xml文件,让类的方法执行

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

public class Demo {
	public static void main(String[] args) throws Exception{
		Document document=new SAXReader().read("D:/daima/exercise/xml/xml/web.xml");
		//通过xpath
		Element element = (Element) document.selectSingleNode("//servlet-class");
		String path = element.getText();
		//反射
		Class clazz=Class.forName(path);
		Method method = clazz.getMethod("add");
		method.invoke(clazz.newInstance());
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42735880/article/details/82942483