Digester Parsing XML Document sample

Source: Internet
Author: User
Tags config final mail
XML parsing XML document

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> Reference Document:</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 a <quasar_module><module_info> node is encountered

* 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




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.