A dynamic loading method with XML configuration, which is more comprehensive than the previous one, and hopefully can also go into the elite area

Source: Internet
Author: User
Tags config
Xml| News | load | Essence FIRST design a simple XML configuration file config.xml
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<test>
<luck title= "Luck Bang Bang Bang" tag= "Luck" application= "dispatch.test" method= "mm" >
</luck>
</test>
The next way to parse XML is to use the Jaxp,dispatch.java
Package dispatch;

Import java.io.*;
Import org.xml.sax.*;
Import Org.xml.sax.helpers.DefaultHandler;
Import Javax.xml.parsers.SAXParserFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;

public class Dispatch extends DefaultHandler
{
Public Dispatch () {
public void execute () {
DefaultHandler handler = new Dispatch ();
SAXParserFactory factory = Saxparserfactory.newinstance ();
try {
SAXParser saxparser = Factory.newsaxparser ();
Saxparser.parse (New File ("Dispatch/config.xml"), handler);

catch (Throwable t) {
T.printstacktrace ();
}

}
Dynamicloader loader = new Dynamicloader ();
//===========================================================
SAX Documenthandler Methods
//===========================================================

public void Startelement (String NamespaceURI,
String LName,//local name
String QName,//qualified name
Attributes attrs)
Throws Saxexception
{
debug//
String Ftag = "LUCK";
////////
String ename = lName; Element name
if ("". Equals (ename)) ename = QName; Namespaceaware = False
if (attrs!= null) {
for (int i = 0; i < attrs.getlength (); i++) {
if (Ftag.equals (Attrs.getvalue (i))) {
System.out.println (Loader.loader) (Attrs.getvalue (i+1), Attrs.getvalue (i+2), "The It???");
Break
}//if equals
}//for I
}//if!null
}
public static void Main (String argv[]) {
Dispatch get = new dispatch ();
Get.execute ();
}
}
One more dynamic loading method Dynamicloader.java
Package dispatch;
Import java.lang.*;
Import java.lang.reflect.*;

public class dynamicloader{
Public Dynamicloader () {
}


public string Loader (string appname,string mthname,string ctn) {
String restr = "";
Class Torun = null;

try{
Torun = Class.forName (appName);
Method Ftogo = Torun.getdeclaredmethod (mthname,new class[]{string.class});
Restr = Ftogo.invoke (Torun.newinstance (), New Object[]{ctn}). toString ();
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Loader:" +e+ "|") +mthname+ "|" +appname+ "|" +CTN);
}
return restr;
}
}
A small debug script, just to return the parameter Test.java
Package dispatch;
public class test{
public string mm (string str) {
return str;
}
}
Parsing an XML file requires a system plus JAXP, JAXR, JAXM, JAXRPC, and a specific look at the SAX documentation. I'm not going to go into the details here.
This one can run, just too slow, with colleagues using the same principle. propertier file way is slower than like, depressed ~

Related Article

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.