Java reflection get XML element

Source: Internet
Author: User
Tags gettext

Class Name:

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()");    }}

Configuration file:

<?xml version="1.0" encoding="UTF-8"?><root>    <class>        <className>day2.mode1.Person</className>        <methodName>jump</methodName>        <argType>java.lang.String</argType>        <argValue>李玟</argValue>    </class></root>

Test class:

Import Java.io.file;import java.lang.reflect.method;import Org.dom4j.document;import Org.dom4j.io.SAXReader;import        org.junit.test;//use DOM4J and reflection technology public class Demo1 {//parse XML file @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);        Reflection creates an object and executes the method Class Clazz = Class.forName (className);        method = Clazz.getmethod (Methodname,null);    Method.invoke (clazz.newinstance (), NULL); }//Parse XML file parameter @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);            Parameter String Argtype = Document.selectsinglenode ("//argtype"). GetText ();            String Argvalue = Document.selectsinglenode ("//argvalue"). GetText ();            Reflection creates an object and executes the method Class Clazz = Class.forName (className);            method = Clazz.getmethod (Methodname,class.forname (Argtype));        Method.invoke (Clazz.newinstance (), argvalue); }}

Java reflection get XML element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.