1. Digester allows you to configure an XML file to be converted to a Java object.ProgramWhen some XML
Special properties will trigger some actions called rules, some of which have been defined,
Of course, you can also set up what you need. digester also includes the following advanced features: * You can embed your original method without affecting your needs.
* Custom namespace-aware execution allows you to define the proper uniqueness of rules. Special XML
Namespace.
* Putting all the rules into rulesets can be easily and repeatedly used in other projects that require the same type. .
Three concepts to be understood:
Pattern is the rule (pattern) that contacts XML)
Example: Pattern // This is a rule
<Datasources> 'datasources'
<Datasource> 'ces CES/datasource'
<Name/> 'ces/datasource/name'
<Driver/> 'ces CES/datasource/driver'
</Datasource>
<Datasource> 'ces CES/datasource'
<Name/> 'ces/datasource/name'
<Driver/> 'ces CES/datasource/driver'
</Datasource>
</Datasources>
When the handler finds the pattern, it will call the associated rule.
Rule)
When pattern matches, it will get the call
The default rule is created by a subclass of org. Apache. commons. digester. Rule.
Object Stack)
Objects are executed only when they are put in this area.
2. Before using digester, you must first have some libraries in your classpath,
Commons-digester, commons-beanutils, commons-collections, commons-logging,
And XML Parser or JAXP (Java API for XML) 2.1 compliant
XML parsing) 1.2.1. I suggest you capture crimson and xerces. The current version is 1.5.
3. digester processing process (rule, pattern, object stack)
Create a new object Method for creating a response rule: Void addobjectcreate (Java. Lang. String Pattern, java. Lang. String classname, Java. Lang. String attributename) // Create a call Method
Void addcallmethod (Java. Lang. String Pattern, java. Lang. String methodname, int Paramcount) // Create parameters passed in by calling Method
Void addcallparam (Java. Lang. String Pattern, int paramindex) // XML Parsing
Java. Lang. Object parse (Java. Lang. String URI) throws java. Io. ioexception, Org. xml. Sax. saxexception
4. Methods for customizing rules
When you open the source code directory, there is Digester-rules.dtd.
This DTD file defines how digester parses your imported XML file. You can also Define related
Patterns, written in the digester-rules.xml, so that digester can be parsed according to your needs Data.
In this way, the program must write addobjectcreate, addcallmethod, and so on. Method.
However, I have not seen any projects currently adopt this approach. <? XML version = "1.0"?>
<! Doctype digester-rules system "digester-rules.dtd"> <Digester-rules>
<Object-create-Rule pattern = "*/foo" classname = "foo"/>
<Set-properties-Rule pattern = "*/foo"/>
</Digester-rules> 5. Examples used in struts actionservlet
Protected void initservlet () throws servletexception {
................. // Prepare a digester to scan the web application deployment descriptor
Digester = new digester ();
Digester. Push (this );
Digester. setnamespaceaware (true );
Digester. setvalidating (false ); .................
// Register our local copy of The dtds that we can find
For (INT I = 0; I <registrations. length; I + = 2 ){
URL url = This. getclass (). getresource (registrations [I + 1]);
If (URL! = NULL)
Digester. Register (registrations [I], URL. tostring ());
} // Configure the processing rules that we need
// Set the corresponding elements and execution rules (Rules)
Digester. addcallmethod ("Web-APP/servlet-mapping", "addservletmapping ", 2 );
Digester. addcallparam ("Web-APP/servlet-mapping/servlet-name", 0 );
Digester. addcallparam ("Web-APP/servlet-mapping/url-pattern", 1 ); Inputstream input = NULL;
Try {
// Get/WEB-INF/Web. XML for parsing
Input =
Getservletcontext (). getresourceasstream ("/WEB-INF/Web. xml ");
Digester. parse (input );
} Catch (throwable e ){
Log. Error (Internal. getmessage ("configwebxml"), e );
} Finally {
If (input! = NULL ){
Try {
Input. Close ();
} Catch (ioexception e ){
;
}
}
} ................. /**
* Remember a Servlet Mapping from our Web Application Deployment
* Descriptor, if it is for this servlet.
*
* @ Param servletname The Name Of The servlet being mapped
* @ Param urlpattern the URL pattern to which this servlet is mapped
*/
// When digester is in parse, The addservletmapping method will be executed.
Public void addservletmapping (string servletname, string urlpattern ){ If (log. isdebugenabled ()){
Log. debug ("process servletname =" + servletname +
", Urlpattern =" + urlpattern );
}
If (servletname = NULL ){
Return;
}
If (servletname. Equals (this. servletname )){
This. servletmapping = urlpattern;
} }
} Related bibliography or relatedArticle
* Jakarta commons:
Http://jakarta.apache.org/commons/index.html
* Jakarta commons digester:
Http://jakarta.apache.org/commons/digester.html
* Simplify XML file processing with the Jakarta commons digester:
Http://jakarta.apache.org/commons/digester/api/index.html
If you can use English, read this article:
Http://www.javaworld.com/javaworld/jw-10-2002/jw-1025-opensourceprofile.html |