String path = JsoupDemo1.class.getClassLoader().getResource("com/mujiachao/xml/dtd/student.xml").getPath();
Document document = Jsoup.parse(new File(path), "utf-8");
Elements name = document.getElementsByTag("name");
for (Element element : name) {
System.out.println(element.text());
}
package com.mujiachao;
import com.mujiachao.fanshe.Demo5;
import com.mujiachao.xml.jsoup.JsoupDemo1;
import com.mujiachao.zhujie.MyZhuJie2;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
@MyZhuJie2(classname = "com.mujiachao.fanshe.Monkey",pathname = "climb")
public class sanzhongfangshi {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
//第一种 通过配置文件
Properties properties = new Properties();
ClassLoader classLoader = Demo5.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("com/mujiachao/pro.properties");
properties.load(resourceAsStream);
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
Class<?> aClass = Class.forName(className);
Object o = aClass.newInstance();
Method method = aClass.getMethod(methodName);
method.invoke(o);
//第二种通过反射
MyZhuJie2 annotation = sanzhongfangshi.class.getAnnotation(MyZhuJie2.class);
String classname = annotation.classname();
String pathname = annotation.pathname();
Class<?> aClass1 = Class.forName(className);
Object o1 = aClass1.newInstance();
Method method1 = aClass.getMethod(methodName);
method1.invoke(o1);
//第三种通过xml jsoup解析器
String path = JsoupDemo1.class.getClassLoader().getResource("com/mujiachao/xml/dtd/student.xml").getPath();
Document document = Jsoup.parse(new File(path), "utf-8");
Elements name = document.getElementsByTag("name");
for (Element element : name) {
System.out.println(element.text());
}
}
}