Match the XML configuration file information with Jakarta Commons Digester to form a Java Object with its corresponding XML rule

xiaoxiao2021-03-06  70

Match the XML configuration file information with Jakarta Commons Digester to form a Java Object with its corresponding XML rule file

Note: Here, in a simple example, how to use Digester.

Step 1: Download Digester in http://jakarta.apache.org/commons/index.html page to find Digester Project, and download Commons-Digester-1.6.zip (I am using)

Step 2: The main libcommons-digester.jar: This is the main LIB of Digester (parsing XML in Digester is the SAX method, it is possible to need crimson.jar, do not need in my JBuilder environment, because JDK has taken Analysis of the libmons-beanutils.jar of the XML file) Commons-beanutils.jar: Major public package (see http://www.apache.org) Commons-logging.jar: Its log package, because commons-digester.jar uses this package

Step 3: Writing XML Profile (config.xml), XML rule file (rules.xml) and java object (example.java) 1.config.xml java: / connectionfactory Queue / Email queue / smscdma 1 Televhone email

2. Rules.xml 3. EXAMPLE.JAVAIMPORT JAVA.NET. *; Import Java.util. *;

Import org.apache.commons.digester. *; import org.apache.commons.digester.xmlrules. *;

Public class example {

/ * The following two string variables, strict XML configuration files, look for * / private string queueConnectionFactory = null; private string exchangepublicationID = null;

/ * The following two main demo XML configuration files are forming a collection of * / private hashmap senderqueuemap = new hashmap (); private linkedlist receiverList = new linkedlist ();

Public example () {TRY {

/ * Please place two XMLs on the example of the example, so that this class can find XML file * / url inputURL = getClass (). Getresource ("config.xml"); url ruless = getClass (). Getresource (" Rules.xml "); Digester Digester = digesterLoader.created; Digester.push (this); digester.parse (InputURL.OpenStream ());

} Catch (exception e) {system.out.print ("can not get configuration" E.TOSTRING ()); system.exit (1);} system.out.print ("Get Configuration IS Successful ");

public static void main (String [] args) {Example paramconfig = new Example (); System.out.print (paramconfig.getSenderQueueMap ()); System.out.println (paramconfig.getExchangePublicationID ()); System.out.print Paramconfig.getReceiverList ());

PUBLIC STRING GETQUECONNECTIONFAACTORY () {Return QueueConnectionFactory;

Public HashMap GetSenderQueuemap () {Return SenderQueueMap;

Public string getExchangepublicationId () {return exchangepublicationId;

/ * Add MAP element * / public void addsenderqueue (String name, string value) {senderqueuemap.put (name.tolowercase (), value);}

Public LinkedList GetRecEternalList () {Return ReceiverList;}

Public void setQueueConnectionFactory (string queueconnectionFactory) {this.queueConnectionFactory = queueconnectionfactory;}

/ * Increase the LinkedList element * / public void addRecEverList (string value) {received.addlast (value);}

Public void setsenderqueuemap (have this.senderqueuemap = senderQueuemap;}

Public void setExchangepublicationId (String ExchangePublic) {this.exchangepublicationID = ExchangePublicationId;}

Public void setiveriverList (LinkedList ReceiverList) {this.receiverList = received;}} Description: Please pay attention to the pattern attribute of Rules.xml and the methodname property

Step 4: Run (debugging at JBuilder)

Step 5: (Advanced) Example of DataSource

To configure Oracle as an example, the increased LIB (class12.jar: Oracle JDBC Driver) 1.config.xml User PWD user PWD Thin 192.168.0.59 TCP 1521 Example

2.Rules.xml 3. Java Object

Import javax.sql.datasource; import java.Net. *; import java.util. *;

Import org.apache.commons.digester. *; import org.apache.commons.digester.xmlrules. *; public class example {private static datasource ds = NULL;

Private string dbuser = null; private string dbpass = null;

Public example () {TRY {

/ * Please place two XMLs on the example of the example, so that this class can find XML file * / url inputURL = getClass (). Getresource ("config.xml"); url ruless = getClass (). Getresource (" "rules.xml");

Digester Digester = DigesterLoader.created; Digester.push (this); Digester.Pars (InputURL.OpenStream ());

} Catch (exception e) {system.out.print ("can not get configuration" E.TOSTRING ()); system.exit (1);} system.out.print ("Get Configuration IS Successful ");

Public static void main (String [] args) {example paramconfig = new example (); DataSource DataSource = paramconfig.gets (). getConnection (); // Get a connection, you can see the ability of the database, huh Java. Sql.connection conn = DataSource.getConnection (paramconfig.getdbuser (), paramconfig.getdbpass ());

}

Public DataSource getds () {return ds;} public void setds (DataSource DS) {this.ds = ds;}

Public string getdbpass () {return dbpass;}

Public string getdbuser () {return dbuser;}

Public void setdbpass (string dbpass) {this.dbpass = dbpass;}

Public void setdbuser (string dbuser) {this.dbuser = dbuser;}}

Summary: Digester features are more and more powerful, here I just throw the jade. Java Application Framework --- Spring Dependency Injection is very strong, you can have time to study.

Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.

转载请注明原文地址:https://www.9cbs.com/read-89055.html

New Post(0)