JAKARTA Commons Digester Learning Information (from elsewhere)

xiaoxiao2021-03-06  61

A simple example

Assume that there are two JavaBeans as follows, Foo and Bar, respectively.

package mypackage; public class Foo {public void addBar (Bar bar); public Bar findBar (int id); public Iterator getBars (); public String getName (); public void setName (String name);} public mypackage; public class Bar {Public int getId (); public void setid (int ID); public string gettitle (); public void settitle (String Title);}

Configure with the XML file below

Use the following lines to complete the configuration file resolution work:

Digest analysis code

Comment

Digester digester = new digester ();

Digester.SetValidating (false);

Not performed the legality verification of XML and the corresponding DTD

Digester.addObjectCreate ("foo", "mypackage.foo");

Create a MyPackage.foo object when you encounter and put it on the top of the stack

Digester.AddSetProperties ("foo");

Set the properties of the Foo object that is created according to the attributes of the element (Property)

Digester.adDObjectcreate ("Foo / Bar", "Mypackage.bar");

Create a MyPackage.bar object when you encounter and place it on top of the stack.

Digester.AddSetProperties ("Foo / Bar");

Set the properties of the BAR object that is created according to the attributes of the element (Property)

Digester.AddSetNext ("Foo / Bar", "Addbar", "MyPackage.bar");

When you encounter the child element of , place it in the top of the stack, and call the AddBar method of the second stack top element (Foo object).

Foo foo = (foo) digester.parse ();

After the analysis, return the root element.

Examples in digester package

****************** ******** < Name> Gonzo gonzo@muppets.com Male kermit kermit@muppets.com kermie@acme.com ********** person.java ******************* Import java.util.hashmap; import java.util.ITerator; Public Class Person {Private Int; Private String Category; Private String Name; Private HashMap Emails = New HashMap (); // In the name of the two methods of the following, the part of the set of the is designed. When identifying the from the XML file, if there is a request (that is, the AddSetProperties method) is required, Digester will automatically call the corresponding method according to this description. Public void setid (int id) {this.id = ID;} public void setcrategory;} // For Name, because of its value from the tag rather than attribute Value, you need to use AddCallMethod to specify the to call this method (you want to automatically call, you need AddbeanPropertySetter, see the next example). Public void setName (String name) {this.name = name;} // with Name, at this time, you will also specify the source of the parameter value of Addemail.

Public void addemail (String Type, string address) {emails.put (type, address);} public void print () {system.out.println ("Person #" ID); system.out.println ("category = " Category; system.out.println (" name = " name); for (Iterator i = emails.keyset (). Iterator (); I.hasNext ();) {String Type = (String) i. NEXT (); string address = (string) emails.get (type); system.out.println ("Email (Type"): " address);}} ******** ******************* Import java.util.LinkedList; import java.util.ITerator; public class addressbook {linkedList people = new linkedList (); public void addperson (Person P) { People.addlast (P); PUBLIC VOID Print () {System.out.Println ("Address Book Has" People.Size () "Entries");

For (iTerator i = people.iterator (); I.hasNext ();) {Person P = (Person) i.next (); p.Print ();}}} ********** ** AddressBookDigester ********* import org.apache.commons.digester.Digester; / ** * Usage: java Example1 example.xml * / public class AddressBookDigester {public static void main (String [] args) {If (args.length! = 1) {usage (); system.exit (-1);} String filename = args [0]; // Create a Digester instance Digester D = new digester (); // Create AddressBook Example and press it into the top of the stack. AddressBook Book = New AddressBook (); D.Push (Book); // Add Rule AddRules (d); // Processing Enter XML file try {java.io.file srcfile = new java.io.file (filename); D.PARSE (Srcfile);} catch (java.io.Exception} {system.out.println ("Error Reading Input" IoE.GetMessage ()); System.exit (-1);} catch Org.xml.sax.saxexception se) {system.out.println ("Error Parsing Input" se.getMessage ()); system.exit (-1);} // Print the resolution address data Book.print ();} private static void addrules (Digester D) {// When you encounter , create an instance of class Person, and press it into the top D. AddObjectCreate ("Address-Book / Person "Person.class); // PRISON> Tribute The properties of the Person> Top Person class objects are mapped according to their respective names, (for example, the label attribute ID with the property setting method) SetID Map, map the tag properties Category with the property setting method setcategory, and then pass the value of the attribute to the performed method. // If a tag property cannot find a corresponding property setting method through the name, this tag property is ignored (such as the first of the first ) in Example.xml).

D. ADSETPROPERTIES ("Address-Book / Person"); // Call the AddPerson method of the second stack of top objects (AddressBook instance), with the object of the stack object (Person instance) as the parameter d.AddSetNext ("Address-Book / Person "," Addperson "); // When encountering the child element of , call the setName method of the Stack Top Object (Person Instance). // The first parameter of the addCallMethod method is the rule. The second parameter is the name of the method. The third is the number of parameters (0, indicating that there is only one parameter, and the value of the parameters is the content of the element) D. AddCallMethod ("Address-Book / Person / Name", "SetName", 0); // When encountering the child element of , call the addemail method of the Stack Top Object (Person Instance), the Addemail method has Two parameters, values ​​from the value of the attribute TYPE from , and the content of itself. // The first parameter of the AddCallParam method is the rule. The second parameter is the serial number of the called method (addemail) parameter, the third is the name of the attribute when the parameter is a string) D.AddCallMethod ("Address- Book / Person / Email "," Addemail ", 2); D.AddCallParam (" Address-Book / Person / Email ", 0," Type "); D.AddCallParam (" Address-Book / Person / Email ", 1 }

Private static void usage () {system.out.println ("usage: java example1 example);}}

The results of the operation are as follows (XML-Crimson, which may require XML-CRIMSON, one source Sun's XML parser, can go to http://xml.apache.org/crimson/ download)

Address book has 2 entriesPerson # 1 category = acquaintance name = Gonzo email (type business): gonzo@muppets.comPerson # 2 category = rolemodel name = Kermit email (type business): kermit@muppets.com email (type home): kermie @ ACME.COM

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

New Post(0)