Use Digester to parse an XML document example

zhaozj2021-02-11  179

Use Digester to parse an XML document example

l_walker 2003-5-2

Keywords: XML, Digester, resolution

abstract:

This paper briefly introduces and exemplifies how to use Apache's Digester to parse the XML document.

1 Overview

Analysis of the XML document has a lot of methods, there are also a lot of toolkits available, here is the Digester from Apache Jakarta, which is very convenient to use it to parse the XML file without excessive care of the specific parsing process.

DiGester first appeared in Struts, later followed by the development of Struts and its publicity, it was reached in Commens, and its underlying achieved SAX analysis. The current version is: 1.5

2. Text

1. Installation and configuration

Using Digester requires the following packages:

Digester, BeanUtils, Collectes, Commens-Logging, and an XML parser that follows SAX (Simple API for XML 2.0 or JAX (Java API for XML Parsing) 1.1 specification, such as Xerces. See the address of the download address. If you need to use the log4j as a log output, please also download it.

Pack the downloaded ZIP and copy the JAR file into the lib directory of your project file, and contain the path to the JAR file described above in the project classpath.

Create a commons-logging.properties file in your engineering class (such as Classes), the content is:

Org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jlogger

This indicates that the system will use log4j as a log output, then build a log4j.proerties file, please refer to the log4j document or other reference.

2. XML file

In the engineering directory, create a new XML file, the example uses module.xml, see below:

Skyhome

The Web Site System of www.skyinn.org

2.0.1

l_wakler

Walker@skyinn.org

2003-04-30

RequestPath = "/ home" actionclass = "org.skyinn.action.globalAction">

RequestPath = "/ forum" ActionClass = "Org.skyinn.Action.forumAction">

3. Analysis XML document

Digester's use is quite simple, please see the comments in the following Sampledigester class, no longer 拗 拗:

/ * ================================================================================================================================================================ ============

* Copyright: www.skyinn.org (c) 2002 - 2003 All rights reserved. * File: org.skyinn.quasar.config.sampledigester

* Inculde: Sampledigester

* Modify Records

* Date Author Content

* =========================================================================================================================================================================================== ============

* 2003-5-1 Walker Create Class

* =========================================================================================================================================================================================== ============ * / package org.skyinn.quasar.config;

Import org.apache.commons.digester.digester; import org.xml.sax.saxexception; import org.skyinn.quasar.Action.ActionMapping;

Import java.io.ioException;

/ **

* Digester parses the XML sample program.

*

* @Author Walker (Walker@skyinn.org)

*

* references:

* Learning and use Jakarta Digester

* Simplify XML configuration file processing with Digester

* / public class samplededeter {

PRIVATE STRING CONFIGFILE;

Public void setconfigfile (final string configfile) {this.configfile = configfile;}

/ **

* Start resolution.

*

* In this method, new Digester instance, and press instance of this class (Sampledigester)

* DiGester's processing stack, calling Digester's AddCallMethod method to make specific XML files

* Node is linked to the processing method, and the bucket addCallParam method sets the parameters, then parse the given XML

*file.

* @Throws ioException IO unusually

* @Throws SAXEXCEPTION SAX exception

* / Public void run () throws oException, SAXEXCEPTION {// New Digester instance

Digester Digester = New Digester (); // This Method Pushes this (Sampledigester) Class to The Digester // Object Stack Making Its Method S Available To Processing Rules.

Digester.push (this); / * quasar_module / module_info is the node path in the XML file:

...

* AddModuleInfo is a method in this class (see below), that is, when you encounter

Node

* Call the AddModuleInfo method, 6 indicates that the method uses six parameters,

* /

Digester.addCallMethod ("QUASAR_MODULE / MODULE_INFO", "AddModuleInfo", 6); // Set parameters one by one, the index of the first parameter is 0

digester.addCallParam ( "quasar_module / module_info / name", 0); digester.addCallParam ( "quasar_module / module_info / description", 1); digester.addCallParam ( "quasar_module / module_info / version", 2); digester.addCallParam ( " quasar_module / module_info / author ", 3); digester.addCallParam (" quasar_module / module_info / mail ", 4); digester.addCallParam (" quasar_module / module_info / update_time ", 5); // This method starts the parsing of the document .

Digester.Parse (this.configfile);} // end run ()

/ **

* Add module information.

*

* This method simply outputs simple output in the XML file, and the obtained information can be further processed in the actual application.

*

* @Param Name name

* @Param Description Description

* @Param Version version

* @Param Author

* @Param Mail Email

* @Param UpdateTime Update Time

* / Public void addModuleInfo (Final String Name, Final String Description, Final String Version, Final String Author, Final String Mail, Final String Updatetime) {// Output

System.out.println ("Name =" Name ", Description =" Description ", Version =" Version ", Author =" Author ", Mail =" Mail ", UpdateTime =" UpdateTime);} // end addmoduleinfo () / **

* Add an action mapping.

*

* This method simply outputs the information of incoming ActionMApping, which can be added to the system Action mapping collection in the specific application.

* @Param anctionsMapping action mapping

* /

Public void addactionmapping {system.out.println (actionmapping);

Public static void main (STRING [] args) {Sampledigester SD = new sampledigester (); sd.setconfigfile ("module.xml"); try {sd.run (); sd = null;} catch (Exception E) {E .printstacktrace ();

// ________________________________________

/ / Demo another analysis method

Digester Digester = New Digester (); Digester.SetValidating (false); // Generate this type of instance

Digester.adDObjectCreate ("QUASAR_MODULE / ACTION_MAPPINGS", SampleDigester.class; // Generate an ActionMApping instance

Digester.adDObjectCreate ("QUASAR_MODULE / ACTION_MAPPINGS / Action", ActionMapping.class); // Get the attribute value and store it in an actionMApping instance

digester.addSetProperties ( "quasar_module / action_mappings / action", "name", "name"); digester.addSetProperties ( "quasar_module / action_mappings / action", "requestPath", "requestPath"); digester.addSetProperties ( "quasar_module / action_mappings / action "," actionclass "," actionclass "); // Call the addActionMApping method for Sampledigester

Digester.addsetNext ("QUASAR_MODULE / ACTION_MAPPINGS / Action", "addActionMApping"); try {digester.parse ("Module.xml");} catch (exception e) {E.PrintStackTrace ();}} // end main )} // Eoc Sampledigester

ActionMapping code list is not given by too long, please write it yourself, just make it three properties: name, actionclass, requestpa, and their getter, setter methods, and toString methods, or you can get QUASAR on my website. Code is found online in line browsing:

http://www.skyinn.org

This paper briefly describes how to use Apache Digester to parse XML files, using it and XML combinations to configure the configuration system. For details, please refer to QUASAR's config package in QUASAR:

http://quasar.skyinn.org

3. References

Learning and use Jakarta Digester:

http://www.onjava.com/pub/a/onjava/2002/10/23/digester.html

Simplify the XML configuration file with Digester:

Http://developer.ccidnet.com/pub/disp/Article?columnid=340&articleid=33259&pageno=1

4. Appendix

Digester, BeanUtils, Collectes, Commens-logging Downloads:

http://jakarta.apache.org/site/binindex.cgi

Log4j download address:

http://jakarta.apache.org/log4j/docs/index.html

XERCES download address:

http://xml.apache.org/xerces2-j/index.html

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

New Post(0)