Read configuration file operation object

Source: Internet
Author: User

I learned the reflection knowledge and the dom4j Parsing Method for XML files. The following is a small case that combines the usage of the two knowledge types:

Package CN. WWH. www. xml. do4j. exmple;

/**
* Category:
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 10:43:00
*/
Public class student {


Private string name;
Private int age;
Public student (){}


Public student (string name, int age ){
Super ();
This. Name = Name;
This. Age = age;
}


Public String getname (){
Return name;
}


Public void setname (string name ){
This. Name = Name;
}


Public int getage (){
Return age;
}


Public void setage (INT age ){
This. Age = age;
}

Public void eat (string name, integer age ){
System. Out. println (name + "this year" + age + "years old ");
System. Out. println ("he is a good guy! ");
}

Public void run (string name ){
System. Out. println (name + "Favorite sports ");
}
}


The file contains class information (student. XML ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Student>
<Classname> CN. WWH. www. xml. do4j. exmple. Student </classname>
<Methodname> eat </methodname>
<Argnametype> JAVA. Lang. String </argnametype>
<Argnamevalue> one boat </argnamevalue>
<Argagetype> JAVA. Lang. Integer </argagetype>
<Argagevalue> 22 </argagevalue>
</Student>
</Root>


Main program code (dom4jparseconfig. Java ):

Package CN. WWH. www. xml. do4j. exmple;

Import java. Lang. Reflect. method;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. element;
Import org. dom4j. Io. saxreader;
Import org. JUnit. test;

/**
* Category:
* Parse data from the configuration file and use reflection to perform object operations
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 10:42:33
*/
Public class dom4jparseconfig {
Private string filepath = "src/CN/WWH/www/XML/do4j/exmple/student. xml ";
@ Test
Public void testdemo () throws exception {
// Use dom4ji to parse the configuration file
Saxreader reader = new saxreader ();
Document document = reader. Read (filepath );
String classname = Document. selectsinglenode ("// classname"). gettext ();
String methodname = Document. selectsinglenode ("// methodname"). gettext ();
String argnametype = Document. selectsinglenode ("// argnametype"). gettext ();
String argnamevalue = Document. selectsinglenode ("// argnamevalue"). gettext ();
String argagetype = Document. selectsinglenode ("// argagetype"). gettext ();
String argagevalue = Document. selectsinglenode ("// argagevalue"). gettext ();

// Obtain the object Using Reflection
Class clazz = Class. forname (classname );
// Obtain Method
Method method = clazz. getmethod (methodname, class. forname (argnametype ),

Class. forname (argagetype ));
Method. Invoke (clazz. newinstance (), argnamevalue, integer. parseint (argagevalue ));
}
}

Output result:

Ye Binzhou is 22 years old.
He is a cute guy!



Note: 1. in clazz. the last two parameters in getmethod (,) should be of the class type. Because the argnametype type is of the string type. lang. string "class. forname () to class type

2. the second parameter in eat (string, integer) should be of the int type, but Int. class is int and cannot be converted in the test code. Therefore, only integer type can be used. In the XML file, its type is Java. lang. integer.

3. document. selectsinglenode ("// argagevalue "). the return value of gettext (); is of the string type. Therefore, you must convert the string type to int type data during the invoke () call.




Read configuration file operation object

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.