java 反射解析xml_java反射获取xml元素
類名:
class Person {
public void run(String who){
System.out.println("Person::run()" + who);
}
public void jump(String who){
System.out.println("Person::jump()" + who);
}
public void run(){
System.out.println("Person::run()");
}
public void jump(){
System.out.println("Person::jump()");
}
}
配置文件:
day2.mode1.Person
jump
java.lang.String
李玟
測試類:
import java.io.File;
import java.lang.reflect.Method;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.junit.Test;
//使用dom4j和反射技術
public class Demo1 {
//解析xml文件
@Test
public void fanse() throws Exception
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("src/day2/mode1/config.xml"));
String className = document.selectSingleNode("//className").getText();
String methodName = document.selectSingleNode("//methodName").getText();
//System.out.println(className + ":" + methodName);
//反射創建對象并執行方法
Class clazz = Class.forName(className);
Method method = clazz.getMethod(methodName,null);
method.invoke(clazz.newInstance(),null);
}
//解析xml文件 參數
@Test
public void fanse2() throws Exception
{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("src/day2/mode1/config.xml"));
String className = document.selectSingleNode("//className").getText();
String methodName = document.selectSingleNode("//methodName").getText();
//System.out.println(className + ":" + methodName);
//參數
String argType = document.selectSingleNode("//argType").getText();
String argValue = document.selectSingleNode("//argValue").getText();
//反射創建對象并執行方法
Class clazz = Class.forName(className);
Method method = clazz.getMethod(methodName,Class.forName(argType));
method.invoke(clazz.newInstance(),argValue);
}
}
總結
以上是生活随笔為你收集整理的java 反射解析xml_java反射获取xml元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java is开头版本解决_eclips
- 下一篇: QQ迎来24岁生日:首位注册用户找到了