Using Digester to parse an XML document sample

Source: Internet
Author: User
Tags add config end final log string version log4j
Xml| sample

I. Overview

There are many ways to parse XML documents, and there are a number of toolkits available, and here is the digester from Apache Jakarta, which is very handy for parsing XML files without the need to be overly concerned with the underlying concrete parsing process.

Digester first appeared in struts, and later with the development of struts and its common nature is mentioned in the Commens, the bottom of the implementation is sax parsing. The current version is: 1.5

  Two. Text

1. Installation and Configuration

The following packages are required to use Digester:

Digester, Beanutils, collections, commens-logging, also has an X that follows sax (simple APIs for XML) 2.0 or JAXP (Java API for XML parsing) 1.1 specification ML parser, such as Xerces. See appendix for the relevant download address. If you need to use log4j as log output, please also download it.

Unzip the downloaded zip package and copy the jar file to the Lib directory of your project file and include the path to the above jar file in the project Classpath.

Create a new Commons-logging.properties file in your project's class directory (such as classes), which reads:

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

This indicates that the system will use log4j as the log output, and then build a log4j.proerties file, see log4j documentation or other reference material.

2. XML file

Create a new XML file in the engineering directory, sample using Module.xml, see below:


!--Module Information-->

Skyhome
the Web site system of www.skyinn.org
2.0.1
L_wakler
walker@skyinn.org
2003-04-30

!--Action Mapping Information-->

Actionclass= "Org.skyinn.action.GlobalAction" >


Requestpath= "/forum"
Actionclass= "Org.skyinn.action.ForumAction" >



3. Parsing XML documents

The use of digester is fairly straightforward, please refer to the comments in the Sampledigester class below, which are not discussed here:

/*============================================================
* 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 parse XML sample program.
* <p>
* @author Walker (walker@skyinn.org)
* <p>
* <strong> References: </strong> <p>
* <a Href= "http://www.onjava.com/pub/a/onjava/2002/10/23/digester.html" target= "_blank" >learning and Using Jakarta Digester </a> <p>
* <a Href= "Http://developer.ccidnet.com/pub/disp/Article?columnID=340&articleID=33259&pageNO=1" target= "_ Blank "> simplifies XML configuration file processing with Digester </a>
*/

public class sampledigester{
Private String configfile;
public void Setconfigfile (final String configfile) {
This.configfile = ConfigFile;
}

/**
* Start parsing.
* <p>
* In this method, new an instance of Digester, and the instance of this class (Sampledigester) is pressed into
*digester the processing stack of the digester, calls the Addcallmethod method of the XML file to place the specific
* nodes are associated with the processing method, and the bucket Addcallparam method sets the parameters and then parses the given XML
File
* @throws IOException IO exception
* @throws saxexception Sax exception
*/

public void Run () throws IOException, saxexception{
New Digester Instance
Digester digester = new Digester ();
This method pushes this (Sampledigester) class to the Digesters
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: <quasar_module> <module_info> ...
*addmoduleinfo is a method in this class (see below), that is, when you encounter the <quasar_module> <module_info> node
* Call the Addmoduleinfo method, 6 means the method uses six parameters,
*/
Digester.addcallmethod ("Quasar_module/module_info", "Addmoduleinfo", 6);
Set parameters individually, with index 0 of the first parameter
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.
* <p>
* This method will simply output the content in the XML file, and the information obtained can be further processed in practical application.
*
* @param name
* @param Description Description
* @param version
* @param author 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 map.
* <p>
* This method simply outputs the information of the incoming actionmapping and can be added to the System action mapping collection in the application.
* @param actionmapping Action Mapping
*/

public void addactionmapping (final actionmapping actionmapping) {
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 ();
}

//__________________________________________
Demonstrate another method of parsing

Digester digester = new Digester ();
Digester.setvalidating (FALSE);
Raw Cost class instance
Digester.addobjectcreate ("Quasar_module/action_mappings", Sampledigester.class);
Generate Actionmapping Instance
Digester.addobjectcreate ("Quasar_module/action_mappings/action", Actionmapping.class);
Gets the property value and stores it in the 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 Sampledigester addactionmapping method
Digester.addsetnext ("Quasar_module/action_mappings/action", "addactionmapping");
try{
Digester.parse ("Module.xml");
}catch (Exception e) {
E.printstacktrace ();
}
}//end Main ()
}//EOC Sampledigester

Actionmapping code listings because it's too long here, write it yourself, just make it three properties: Name,actionclass,requestpath and their getter, setter, and ToString methods.



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.