Jakarta commons digester introduction and usage

Source: Internet
Author: User
Tags xml parser
Jakarta commons digester introduction and usage
2004-02-27 Browsing times: 757

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

Comment
Related Materials
  • efficient parsing of XML
  • select and recognize the XML character encoding method
  • Jakarta commons Digester (1) 2004-01-12
  • Jakarta commons digester (2) 2004-01-12
  • Jakarta commons digester (3) 2004-01-12
  • 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.